利用 RAPI.DLL文件,如何从PDA机器上读取文件和从PC上把一个txt文件放到PDA上去..
环境VS2005,装有activesync,wince 5.0模拟器,又从网上下了rapi.dll 库,界面上有一测试按钮.
后台代码定义了类RAPI和它的方法,引用中也引用了
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.IO;
代码如下
public class RAPI
     {  
         private const uint GENERIC_WRITE = 0x40000000;  // 设置读写权限
         private const short CREATE_NEW = 1;    // 创建新文件
         private const short FILE_ATTRIBUTE_NORMAL = 0x80;  // 设置文件属性  
         private const short INVALID_HANDLE_VALUE = -1;  // 错误句柄  
         IntPtr remoteFile = IntPtr.Zero;  
         String LocalFileName = @"d:\test.txt";   // 本地计算机文件名  
         String RemoteFileName = @"\My Documents\test.txt";  // 远程设备文件名  
         byte[] buffer = new byte[0x1000];    // 传输缓冲区定义为4k  
         FileStream localFile;  
         int bytesread = 0;  
         int byteswritten = 0;  
         int filepos = 0;  
         public void RapiFile()   
         {   
             // 创建远程文件   
             remoteFile = CeCreateFile(RemoteFileName, GENERIC_WRITE, 0, 0, CREATE_NEW,     FILE_ATTRIBUTE_NORMAL, 0);   
             // 检查文件是否创建成功   
             if ((int)remoteFile == INVALID_HANDLE_VALUE)   
             {    
                 throw new Exception("Could not create remote file");   
             }   
             // 打开本地文件   
             localFile = new FileStream(LocalFileName, FileMode.Open);   
             // 读取4K字节   
             bytesread = localFile.Read(buffer, filepos, buffer.Length);  
             while(bytesread > 0)   
             {    
                 // 移动文件指针到已读取的位置    
                 filepos += bytesread;    
                 // 写缓冲区数据到远程设备文件    
                 if(! Convert.ToBoolean(CeWriteFile(remoteFile, buffer, bytesread,     ref byteswritten, 0)))    
                 {  
                     // 检查是否成功,不成功关闭文件句柄,抛出异常    
                     CeCloseHandle(remoteFile);     
                     throw new Exception("Could not write to remote file");    
                 }    
                 try    
                 {     
                     // 重新填充本地缓冲区     
                     bytesread = localFile.Read(buffer, 0, buffer.Length);    
                 }    
                 catch(Exception)    
                 {     
                     bytesread = 0;    
                 }   
             }   
             // 关闭本地文件   
             localFile.Close();   
             // 关闭远程文件   
             CeCloseHandle(remoteFile);  
         }  
         // 声明要引用的API  
         [DllImport("rapi.dll", CharSet=CharSet.Unicode)]  
         internal static extern int CeCloseHandle(IntPtr hObject);   
         [DllImport("rapi.dll", CharSet=CharSet.Unicode)]  
         internal static extern int CeWriteFile(IntPtr hFile, byte[] lpBuffer,   int nNumberOfbytesToWrite, ref int lpNumberOfbytesWritten, int lpOverlapped);
         [DllImport("rapi.dll", CharSet=CharSet.Unicode, SetLastError=true)]
         internal static extern IntPtr CeCreateFile(  string lpFileName,   uint dwDesiredAcc