WPD MTP stream hangs on commit

我的梦境 提交于 2019-12-12 01:54:17

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!