与WPD文件复制到Windows Phone C# [英] Copy files with WPD to Windows Phone C#
问题描述
我想通过MTP至少一个文件复制到Windows手机。
我能够连接到手机和从手机复制文件到电脑下面这个教程:
的 WPD:传输内容
但是我无法拷贝文件的其他方式轮(从计算机到电话)。
这是我的代码:
I want to copy at least one file to a windows phone via MTP. I am able to connect to the phone and copy files from the phone to the computer following this tutorial: WPD: Transferring Content However I am unable to copy files the other way round (from computer to phone). This is my code:
IPortableDeviceContent content;
this._device.Content(out content);
IPortableDeviceValues values = GetRequiredPropertiesForContentType(fileName, parentObjectId);
PortableDeviceApiLib.IStream tempStream;
uint optimalTransferSizeBytes = 0;
content.CreateObjectWithPropertiesAndData(
values,
out tempStream,
ref optimalTransferSizeBytes,
null);
System.Runtime.InteropServices.ComTypes.IStream targetStream = (System.Runtime.InteropServices.ComTypes.IStream)tempStream;
try
{
using (var sourceStream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
var buffer = new byte[optimalTransferSizeBytes];
int bytesRead;
do
{
bytesRead = sourceStream.Read(buffer, 0, (int)optimalTransferSizeBytes);
IntPtr pcbWritten = IntPtr.Zero;
targetStream.Write(buffer, (int)optimalTransferSizeBytes, pcbWritten);
} while (bytesRead > 0);
}
targetStream.Commit(0);
}
finally
{
Marshal.ReleaseComObject(tempStream);
}
我测试了几个设备,这个代码。它适用于普通的MP3播放器,并假设本教程是正确的,所以也可以在Android手机上。
,而运行这段代码有两个不同的Windows手机,我得到以下异常:
I tested this code on several devices. It works on a normal mp3 player and assuming the tutorial is correct, it also works on an Android phone. But running this code with two different windows phone, I get following exception:
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in PortableDevices.exe
Additional information: The data area passed to a system call is too small. (Exception from HRESULT: 0x8007007A)
在这行: targetStream.Write(缓冲区(INT)optimalTransferSizeBytes,pcbWritten);
缓冲区大小为262144字节,而文件大小只有75 K字节。我希望有人有一个想法如何解决这个问题。
The buffer size is 262144 Byte, while the filesize is only 75 KByte. I hope someone has an idea how to fix this issue.
问候
j0h4nn3s
Greetings j0h4nn3s
推荐答案
我有同样的问题,原来笔者做了一个简单的错误。你写的缓冲区,而不是你一个字节你读的数字的大小。
I had the same issue, turns out the author made a simple mistake. You're writing the size of the buffer instead of the numbers you bytes you read.
替换
targetStream.Write(buffer, (int)optimalTransferSizeBytes, pcbWritten);
按
by
targetStream.Write(buffer, bytesRead, pcbWritten);
这篇关于与WPD文件复制到Windows Phone C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!