Xihuny
Xihuny

Reputation: 1538

PortableDeviceApiLib show progress TransferContentToDevice()

I am using https://www.nuget.org/packages/PortableDevices/ to copy files to WPD.

I can copy files using TransferContentToDevice(___,___) function.

Right now, it's not showing any progress dialog. How can I show transfer progress?

My Code

            //Connect to MTP devices and pick up the first one
            var devices = new PortableDeviceCollection();
            devices.Refresh();

            if (devices.Count > 0)
            {
                device = devices.First();
                device.Connect();

                string rootId = device.GetRootId();
                device.TransferContentToDevice(@"C:\\test\testFile.mp4", rootId);

                //Close the connection
                device.Disconnect();
            }
            else {
                Console.WriteLine("No device connectd");
            }

TransferContentToDevice() Function - From Portable Device API

public void TransferContentToDevice(string fileName, string parentObjectId)
        {
            IPortableDeviceContent content;
            PortableDeviceClass.Content(out content);

            var values = GetRequiredPropertiesForContentType(fileName, parentObjectId);

            uint optimalTransferSizeBytes = 0;
            content.CreateObjectWithPropertiesAndData(values, out PortableDeviceApiLib.IStream tempStream, ref optimalTransferSizeBytes, null);

            var 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);
                        var pcbWritten = IntPtr.Zero;
                        targetStream.Write(buffer, bytesRead, pcbWritten);
                    } while (bytesRead > 0);
                }
                targetStream.Commit(0);
            }
            finally
            {
                Marshal.ReleaseComObject(tempStream);
            }
        }

Upvotes: 0

Views: 631

Answers (1)

Jack J Jun- MSFT
Jack J Jun- MSFT

Reputation: 5986

Based on my search, it seems that we may not find a way to show the progress.

Because we don't how the PortableDevice transfer, like we don't know its Transmission rate,

and maximum and minimum.

You could look at File Copy with Progress Bar to know to how to set the progress bar when copying a file.

Upvotes: 0

Related Questions