using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.Threading;
namespace pctoce
{
public partial class Form1 : Form
{
// 声明要引用的API
[DllImport("rapi.dll")]private static extern uint CeRapiUninit();
[DllImport("rapi.dll")]private static extern uint CeRapiInitEx(ref RAPIINIT pRapiInit);
[DllImport("rapi.dll", CharSet=CharSet.Unicode)] internal static extern int CeDeleteFile(string FileName);
[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 dwDesiredAccess,int dwShareMode,int lpSecurityAttributes,int dwCreationDisposition,int dwFlagsAndAttributes,int hTemplateFile);
[DllImport("rapi.dll", CharSet=CharSet.Unicode)] public static extern int CeFindFirstFile(string lpFileName,ref CE_FIND_DATA lpFindFileData);
[DllImport("rapi.dll", CharSet=CharSet.Unicode)] internal static extern int CeReadFile(int hFile,byte[] lpBuffer,int lpOverlapped,out int laji,int gouride);
[DllImport("rapi.dll", CharSet=CharSet.Unicode)] internal static extern int CeGetFileSize(int hFile, int lpOverlapped);
public struct CE_FIND_DATA
{
public int dwFileAttributes;
public FILETIME ftCreationTime;
public int ftCreationTime2;
public FILETIME ftLastAccessTime;
public FILETIME ftLastWriteTime;
public int nFileSizeHigh;
public int nFileSizeLow;
public int dwOID;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=260)]
public string cFileName;
}
[StructLayout(LayoutKind.Explicit)]
private struct RAPIINIT
{
[FieldOffset(0)] public int cbsize;
[FieldOffset(4)] public System.IntPtr heRapiInit;
[FieldOffset(8)] public System.IntPtr hrRapiInit;
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
CopyFileToPDA(@"C:/Documents and Settings/All Users/Documents/My Pictures/示例图片/Blue hills.jpg", @"/Temp/Blue hills.jpg");
}
private bool InitDevice(int nTimeout)
{
RAPIINIT ri = new RAPIINIT();
ri.cbsize = System.Runtime.InteropServices.Marshal.SizeOf(ri);
uint hRes = CeRapiInitEx(ref ri);
ManualResetEvent me = new ManualResetEvent(false);
me.Handle = ri.heRapiInit;
if (!me.WaitOne(nTimeout, true))
{
CeRapiUninit();
return false;
}
else
{
return true;
}
}
[DllImport("rapi.dll", CharSet = CharSet.Unicode)]
internal static extern int CeRapiGetError();
[DllImport("rapi.dll", CharSet = CharSet.Unicode)]
internal static extern int CeRapiInit();
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;
byte[] buffer = new byte[0x1000];
// 传输缓冲区定义为4k
FileStream localFile;
int bytesread = 0;
int byteswritten = 0;
int filepos = 0;
// 本地计算机文件名 LocalFileName ;
// 远程设备文件名 RemoteFileName;
public bool CopyFileToPDA(string LocalFileName, string RemoteFileName)
{
if (InitDevice(2000))
{
//查找远程文件
CE_FIND_DATA findData = new CE_FIND_DATA();
int jieg = CeFindFirstFile(RemoteFileName, ref findData);
if (jieg != -1)
{
CeDeleteFile(RemoteFileName);
}
// 创建远程文件
remoteFile = CeCreateFile(RemoteFileName, GENERIC_WRITE, 0, 0, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0);
// 检查文件是否创建成功
if ((int)remoteFile == INVALID_HANDLE_VALUE)
{
throw new Exception("创建文件失败!");
}
else
{
// 打开本地文件
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("写远程文件失败!");
}
try
{
// 重新填充本地缓冲区
bytesread = localFile.Read(buffer, 0, buffer.Length);
}
catch (Exception)
{
bytesread = 0;
}
}
// 关闭本地文件
localFile.Close();
// 关闭远程文件
CeCloseHandle(remoteFile);
filepos = 0;
CeRapiUninit();
return true;
}
}
else
{
return false;
}
}
// 设置读写权限
private const short CREATE_OPEN = 3;
private const uint GENERIC_READ = 0x80000000;
// 1 成功 0 失败 -1 没找到文件
public string CopyFileToPC(string RemoteFileName, string LocalFileName)
{
if (InitDevice(2000))
{
IntPtr fileh = IntPtr.Zero;
fileh = CeCreateFile(RemoteFileName, GENERIC_READ, 0, 0, CREATE_OPEN, FILE_ATTRIBUTE_NORMAL, 0);
if ((int)fileh == INVALID_HANDLE_VALUE)
{
return "-1";
}
else
{
try
{
FileStream writer = new FileStream(LocalFileName, FileMode.Create);
int size = CeGetFileSize((int)fileh, 0);
byte[] buff = new byte[400096];
int readed = 400096;
while (readed == 400096)
{
CeReadFile((int)fileh, buff, 400096, out readed, 0);
writer.Write(buff, 0, readed);
}
CeCloseHandle(fileh);
writer.Close();
return "1";
}
catch
{
return "0";
}
CE_FIND_DATA findData = new CE_FIND_DATA();
int jieg = CeFindFirstFile(RemoteFileName, ref findData);
if (jieg != -1)
{
CeDeleteFile(RemoteFileName);
}
}
}
else
{
return "0";
}
}
private void button2_Click(object sender, EventArgs e)
{
CopyFileToPC(@"/Temp/6901028310192.jpg", @"c:/6901028310192.jpg");
}
}
}