使用RAPI库操作移动设备——C#怎麼將設備上的某個文件(\Temp)复制到PC(如C:盤)下,前提是如何利用RAPI库,急救!急救! 大家好! 使用RAPI库操作移动设备——C#怎麼將設備上的某個文件(\Temp)复制到PC(如C:盤)下,前提是如何利用RAPI库,急救!急救! 謝謝!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 找到解決答案了: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(); }} 条件using 组数高效筛选,高级题,高手进 .NET Framework版本问题 Winform 程序内存跟踪 怎么判断固定时间内没有出发时间 求助:TCPClient NetworkStream 使用问题 C#combobox的属性为dropdownlist 求教图片像素值问题 急!子窗口如何控制父窗口的空间以及使用其函数? 软件所的兄弟请进 c#做视屏、图象处理合适吗? 倾我所有 求 金旭亮 《编程的奥秘》书中光盘里的源代码
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();
}}