现在的位置: 首页 > 综合 > 正文

C#怎么将设备上的某个文件(/Temp)复制到PC(如C:盘)下

2013年08月22日 ⁄ 综合 ⁄ 共 3099字 ⁄ 字号 评论关闭
  using   System.Runtime.InteropServices;  
  using   System.Runtime.ConstrainedExecution;  
  using   System.Runtime.Serialization;  
   
  public   class   PDA{  
  [DllImport("rapi.dll",   CharSet   =   CharSet.Unicode,   SetLastError   =   true)]  
                  internal   static   extern   IntPtr   CeCreateFile(  
                          string   lpFileName,  
                          uint   dwDesiredAccess,  
                          int   dwShareMode,  
                          int   lpSecurityAttributes,  
                          int   dwCreationDisposition,  
                          int   dwFlagsAndAttributes,  
                          int   hTemplateFile);  
                  [DllImport("rapi.dll",   CharSet   =   CharSet.Unicode,   SetLastError   =   true)]  
                  internal   static   extern   int   CeReadFile(IntPtr   hFile,   byte[]   lpBuffer,   int   nNumberOfbytesToRead,   ref   int   lpNumberOfbytesRead,   int   lpOverlapped);  
   
                  [DllImport("rapi.dll",   CharSet   =   CharSet.Unicode)]  
                  internal   static   extern   int   CeCloseHandle(IntPtr   hObject);    
   
                  private   const   short   CREATE_NEW   =   1;  
                  private   const   short   CREATE_ALWAYS   =   2;  
                  private   const   uint   GENERIC_WRITE   =   0x40000000;  
                  private   const   uint   GENERIC_READ   =   0x80000000;  
                  private   const   short   OPEN_EXISTING   =   3;  
                  private   const   short   FILE_ATTRIBUTE_NORMAL   =   0x80;  
                  internal   const   int   ERROR_NO_MORE_FILES   =   18;  
                  private   const   short   INVALID_HANDLE_VALUE   =   -1;  
  public   const   string   UploadPath   =   "C://FPGMaterial//UPLOAD//";  
  //將遠程設備上的文件複製到PCs文件目錄下  
                  public   void   CopyFileFromDevice(string   LocalFileName,   string   RemoteFileName,   bool   Overwrite)  
                  {  
                          FileStream   localFile;  
                          IntPtr   remoteFile   =   IntPtr.Zero;  
                          int   bytesread   =   0;  
                          int   create   =   Overwrite   ?   CREATE_ALWAYS   :   CREATE_NEW;  
                          byte[]   buffer   =   new   byte[0x1000];   //   4k   transfer   buffer  
   
                          //   open   the   remote   (device)   file  
                          remoteFile   =   CeCreateFile(RemoteFileName,   GENERIC_READ,   0,   0,   OPEN_EXISTING,   FILE_ATTRIBUTE_NORMAL,   0);  
   
                          //   check   for   success  
                          if   ((int)remoteFile   ==   INVALID_HANDLE_VALUE)  
                          {  
                                  throw   new   Exception("不能打開遠程設備上的Material_Tx.xml文件");  
                          }  
   
                          //   create   the   local   file  
                          localFile   =   new   FileStream(LocalFileName,   Overwrite   ?   FileMode.Create   :   FileMode.CreateNew,   FileAccess.Write);  
   
                          //   read   data   from   remote   file   into   buffer  
                          CeReadFile(remoteFile,   buffer,   0x1000,   ref   bytesread,   0);  
                          while   (bytesread   >   0)  
                          {  
                                  //   write   it   into   local   file  
                                  localFile.Write(buffer,   0,   bytesread);  
   
                                  //   get   more   data  
                                  if   (!Convert.ToBoolean(CeReadFile(remoteFile,   buffer,   0x1000,   ref   bytesread,   0)))  
                                  {  
                                          CeCloseHandle(remoteFile);  
                                          localFile.Close();  
                                          throw   new   Exception("讀取遠程設備上的Material_Tx.xml文件失敗!");  
                                  }  
                          }  
   
                          //   close   the   remote   file  
                          CeCloseHandle(remoteFile);  
   
                          localFile.Flush();  
   
                          //   close   the   local   file  
                          localFile.Close();  
                  }  
   
  }

抱歉!评论已关闭.