问题
I am using the following code to copy a file wrapped by a FileInfo object to an MTP device using the Windows Portable Device API:
public static void CopyFileToDevice(PortableDeviceFolder parent, string name, FileInfo file)
{
IPortableDeviceValues values = GetRequiredPropertiesForContentType(parent.Id, name, file.Length);
PortableDeviceApiLib.IStream tempStream;
uint blockSize = 0;
parent.Device.Content.CreateObjectWithPropertiesAndData(
values,
out tempStream,
ref blockSize,
null);
System.Runtime.InteropServices.ComTypes.IStream targetStream =
(System.Runtime.InteropServices.ComTypes.IStream)tempStream;
try
{
using (var sourceStream = file.OpenRead())
{
var buffer = new byte[blockSize];
int bytesRead;
do
{
bytesRead = sourceStream.Read(buffer, 0, (int)blockSize);
targetStream.Write(buffer, bytesRead, IntPtr.Zero);
} while (bytesRead > 0);
}
targetStream.Commit(0);
}
finally
{
Marshal.ReleaseComObject(tempStream);
}
parent.Refresh();
}
Now, this works quite fine, however when writing a small file, in this case a text-only .m3u file of a couple of kiB, the line
targetStream.Commit(0);
takes extremely long to execute. When writing a file of several MiB, nothing is wrong. I would like to know why this is happening and how I might fix this. Thanks!
来源:https://stackoverflow.com/questions/28136945/wpd-mtp-stream-hangs-on-commit