WPD 从便携设备拷贝文件到PC文件不完整的解决办法
来源:互联网 发布:代理商域名转到阿里云 编辑:程序博客网 时间:2024/05/21 14:50
起初从网上查询到的由便携设备拷贝文件到PC的方法如下:
- private void TransferContentFromDevice(string parentObjectID, string saveToPath)
- {
- string folder = System.IO.Path.GetDirectoryName(saveToPath);
- if (!Directory.Exists(folder))
- {
- Directory.CreateDirectory(folder);
- }
- if (File.Exists(saveToPath))
- {
- File.Delete(saveToPath);
- }
- IPortableDeviceContent content;
- ppDevice.Content(out content);
- IPortableDeviceResources resources;
- content.Transfer(out resources);
- PortableDeviceApiLib.IStream wpdStream;
- uint optimalTransferSize = 0;
- PortableDeviceApiLib._tagpropertykey property = new PortableDeviceApiLib._tagpropertykey();
- property.fmtid = new Guid(0xE81E79BE, 0x34F0, 0x41BF, 0xB5, 0x3F, 0xF1, 0xA0, 0x6A, 0xE8, 0x78, 0x42);
- property.pid = 0;
- resources.GetStream(parentObjectID, ref property, 0, ref optimalTransferSize, out wpdStream);
- System.Runtime.InteropServices.ComTypes.IStream sourceStream = (System.Runtime.InteropServices.ComTypes.IStream)wpdStream;
- string filename = Path.GetFileName(saveToPath);
- FileStream targetStream = new FileStream(saveToPath, FileMode.Create, FileAccess.Write);
- {
- int filesize = int.Parse(optimalTransferSize.ToString());
- var buffer = new byte[filesize];
- int bytesRead = 0;
- do
- {
- sourceStream.Read(buffer, filesize, new IntPtr(bytesRead));
- targetStream.Write(buffer, 0, filesize);
- } while (bytesRead > 0);
- targetStream.Close();
- targetStream.Dispose();
- }
- //若不添此行,在本方法执行一次后再次执行时会报资源占用错误
- Marshal.ReleaseComObject(wpdStream);
- }
解决:
- private void TransferContentFromDevice(string parentObjectID, string saveToPath)
- {
- string folder = System.IO.Path.GetDirectoryName(saveToPath);
- if (!Directory.Exists(folder))
- {
- Directory.CreateDirectory(folder);
- }
- if (File.Exists(saveToPath))
- {
- File.Delete(saveToPath);
- }
- IPortableDeviceContent content;
- ppDevice.Content(out content);
- IPortableDeviceResources resources;
- content.Transfer(out resources);
- PortableDeviceApiLib.IStream wpdStream;
- uint optimalTransferSize = int.MaxValue;
- PortableDeviceApiLib._tagpropertykey property = new PortableDeviceApiLib._tagpropertykey();
- property.fmtid = new Guid(0xE81E79BE, 0x34F0, 0x41BF, 0xB5, 0x3F, 0xF1, 0xA0, 0x6A, 0xE8, 0x78, 0x42);
- property.pid = 0;
- resources.GetStream(parentObjectID, ref property, 0, ref optimalTransferSize, out wpdStream);
- System.Runtime.InteropServices.ComTypes.IStream sourceStream = (System.Runtime.InteropServices.ComTypes.IStream)wpdStream;
- string filename = Path.GetFileName(saveToPath);
- FileStream targetStream = new FileStream(saveToPath, FileMode.Create, FileAccess.Write);
- {
- int filesize = int.Parse(optimalTransferSize.ToString());
- var buffer = new byte[filesize];
- int bytesRead = 0;
- IntPtr bytesReadIntPtr = new IntPtr(bytesRead);
- //设备建议读取长度optimalTransferSize长度一般为262144即256k,
- //注释掉的sourceStream.Read不能更新bytesRead值,do循环只能执行一次即写入256k数据。
- //创建nextBufferSize变量,用于每次Read后计算下一次buffer长度
- int nextBufferSize = 0;
- do
- {
- if (bytesReadIntPtr == IntPtr.Zero)
- {
- bytesReadIntPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(int)));
- }
- //sourceStream.Read(buffer, filesize, new IntPtr(bytesRead));
- sourceStream.Read(buffer, filesize, bytesReadIntPtr);
- nextBufferSize = Marshal.ReadInt32(bytesReadIntPtr);
- if (filesize > nextBufferSize)
- {
- filesize = nextBufferSize;
- }
- targetStream.Write(buffer, 0, filesize);
- } while (nextBufferSize > 0);
- //} while (bytesRead > 0);
- targetStream.Close();
- targetStream.Dispose();
- Marshal.FreeCoTaskMem(bytesReadIntPtr);
- }
- //若不添此行,在本方法执行一次后再次执行时会报资源占用错误
- Marshal.ReleaseComObject(wpdStream);
- }
阅读全文
0 0
- WPD 从便携设备拷贝文件到PC文件不完整的解决办法
- WPD 从便携设备拷贝文件到PC文件不完整的解决办法
- 从PC拷贝文件到磁盘
- C# WPD PortableDeviceApiLib获取便携设备列表
- C# WPD PortableDeviceApiLib获取便携设备列表
- php文件下载不完整的解决办法
- Android设备不root,从App目录下拷贝文件
- 安装ftp服务 ,vsftpd.conf 的使用,把文件从pc 上传到设备
- pc到U盘的文件拷贝小工具源代码
- 程序文件直接拷贝到pda设备 (USB连接pc pda wince6.0 C#)
- PC上传下载文件到WinCE设备
- PC操作移动设备windows CE 包括互相拷贝文件
- 从android虚拟机复制文件到PC的命令
- 从文件到字符设备
- 从文件到字符设备
- 从文件到字符设备
- 从文件到字符设备
- C# V3.5 OpenNETCF实现PC与CE设备的文件拷贝
- Ubuntu C++ 环境的搭建
- Effect Java 阅读笔记(一)
- 图像检索系列一:Deep Learning of Binary Hash Codes for Fast Image Retrieval
- BZOJ 1083: [SCOI2005]繁忙的都市
- POJ:Subsequence
- WPD 从便携设备拷贝文件到PC文件不完整的解决办法
- BDIP-BVLC纹理
- [机器学习入门] 李宏毅机器学习笔记-32 (Recurrent Neural Network part 1;循环神经网络 part 1)
- HDU
- 邻接表数组实现
- CSUACM月赛 1970
- use shadowsocks sync android source code
- 变色DNA
- 最大流 紧急疏散evacuate