【引用】C#上传下载PDA数据
来源:互联网 发布:王小波作品推荐 知乎 编辑:程序博客网 时间:2024/05/11 21:56
一定要安装 MicroSoft 的 MS.ActiveSync 4.1 CHS.rar才行!代码为: using System; using System.Runtime.InteropServices; using System.IO; using System.Threading; namespace PDA { /// <summary> /// Class1 的摘要说明。 /// </summary> public class GuangMa { public GuangMa() { // // TODO: 在此处添加构造函数逻辑 // } 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; } }
0 0
- 【引用】C#上传下载PDA数据
- C# pda 数据采集器 防止休眠
- C# PDA 数据采集器 判断wifi,gprs,activesync状态
- PDA数据采集系统
- PDA开发数据由DB下载至PDA本地
- PDA
- PDA
- PDA
- 引用:jspSmartUpload上传下载全攻略
- 引用:jspSmartUpload上传下载全攻略
- c#实现文件上传下载
- C# WinForm 文件上传下载
- C# FTP文件上传下载
- C# WinForm 文件上传下载
- FTP上传下载 C#
- c# ftp上传下载
- 用C#上传下载文件
- C# WinForm 文件上传下载
- DataTable clone()与ImportRow()应用
- 分页:对已提取数据进行分页
- 公网IP需求,设置
- Unity连接数据库(超级新手版)
- Tomcat6.0配置
- 【引用】C#上传下载PDA数据
- 【引用】MyEclipse6.5安装SVN插件方法
- C# Mobile远程调用WebService时,返回DataTable出现隐式转换出问题的解决方案
- 【引用】struts2中的constant介绍之struts.objectFactory与spring的整合
- Jetty 新手使用日记
- [转]JBoss was unable to start within 50 seconds
- Android图片的获取和存储
- linux select poll epoll
- Spring + jpa 异常处理