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

本文介绍了一种使用C#从远程设备复制文件到个人电脑的方法。通过调用特定的DLL,实现文件的创建、读取及关闭等功能,并详细解释了如何处理文件传输过程中的错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  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();  
                  }  
   
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值