private static string TransferContentFromDevice(string saveToPath, IPortableDeviceContent content, string parentObjectID)
        {
            IPortableDeviceResources resources;

            content.Transfer(out resources);
            PortableDeviceApiLib.IStream wpdStream = null;
            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;

            try
            {
                resources.GetStream(parentObjectID, ref property, 0, ref optimalTransferSize, out wpdStream);
                System.Runtime.InteropServices.ComTypes.IStream sourceStream = (System.Runtime.InteropServices.ComTypes.IStream)wpdStream;

                using (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, bytesReadIntPtr);
                        nextBufferSize = Marshal.ReadInt32(bytesReadIntPtr);
                        if (filesize > nextBufferSize)
                        {
                            filesize = nextBufferSize;
                        }

                        targetStream.Write(buffer, 0, filesize);
                    } while (nextBufferSize > 0);
                    Marshal.FreeCoTaskMem(bytesReadIntPtr);
                }
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }
            finally
            {
                if (wpdStream != null)
                {
                    Marshal.ReleaseComObject(wpdStream);
                }
            }
            return(string.Empty);
        }
Exemple #2
0
        private void DownloadFile(TransFileObject file, string destPath, IPortableDeviceContent content)
        {
            IPortableDeviceProperties properties;

            content.Properties(out properties);

            var downloadFileObj = WrapObject(properties, file.objId);

            IPortableDeviceResources resources;

            content.Transfer(out resources);

            PortableDeviceApiLib.IStream wpdStream;
            uint optimalTransferSize = 0;

            var property = new _tagpropertykey();

            property.fmtid = new Guid(0xE81E79BE, 0x34F0, 0x41BF, 0xB5, 0x3F, 0xF1, 0xA0, 0x6A, 0xE8, 0x78, 0x42);
            property.pid   = 0;

            resources.GetStream(file.objId, ref property, 0, ref optimalTransferSize, out wpdStream);
            System.Runtime.InteropServices.ComTypes.IStream sourceStream = (System.Runtime.InteropServices.ComTypes.IStream)wpdStream;

            FileStream targetStream = new FileStream(destPath, FileMode.Create, FileAccess.Write);

            unsafe {
                var buffer = new byte[10240];
                int bytesRead;
                do
                {
                    sourceStream.Read(buffer, 10240, new IntPtr(&bytesRead));
                    if (bytesRead <= 0)
                    {
                        break;
                    }
                    targetStream.Write(buffer, 0, bytesRead);
                } while (true /*bytesRead > 0*/);

                targetStream.Close();
            }

            Marshal.ReleaseComObject(sourceStream);
            Marshal.ReleaseComObject(wpdStream);


            // ファイルの更新日時を更新
            DateTime setDate = file.updateTime;

            if (setDate.CompareTo(DateTime.MinValue) == 0)
            {
                setDate = GetImgTakenDate(destPath);  // Exifから撮影日時情報を取得
            }
            File.SetLastWriteTime(destPath, setDate);
        }
        /// <summary>
        /// open a stream on the device for reading
        /// </summary>
        /// <param name="deviceContent">unmanged device</param>
        /// <param name="objectId">object to open</param>
        /// <param name="mode">mode to open the stream in</param>
        /// <returns></returns>
        public IStream OpenResourceStream(IPortableDeviceContent deviceContent, string objectId, uint mode)
        {
            IPortableDeviceResources resources;

            deviceContent.Transfer(out resources);

            IStream stream;
            uint    optimalBufferSize = 0;

            resources.GetStream(objectId, PortableDevicePropertyKeys.WPD_RESOURCE_DEFAULT, mode, ref optimalBufferSize, out stream);

            return(stream);
        }
        /// <summary>
        /// open a stream on the device for reading
        /// </summary>
        /// <param name="deviceContent">unmanged device</param>
        /// <param name="objectId">object to open</param>
        /// <param name="mode">mode to open the stream in</param>
        /// <returns></returns>
        public IStream OpenResourceStream(IPortableDeviceContent deviceContent, string objectId, uint mode)
        {
            IPortableDeviceResources resources;
            deviceContent.Transfer(out resources);

            IStream stream;
            uint optimalBufferSize = 0;
            resources.GetStream(objectId, PortableDevicePropertyKeys.WPD_RESOURCE_DEFAULT, mode, ref optimalBufferSize, out stream);            
            //_ULARGE_INTEGER uli = new _ULARGE_INTEGER();
            //uli.QuadPart = GetObjectFileSize(deviceContent, objectId);
            //stream.SetSize(uli);
            return stream;
        }
Exemple #5
0
        /**
         * Copy To PC
         */
        public void TransferContentFromDevice(PortableDeviceFile file, string saveToPath, String fileName)
        {
            FileStream targetStream = null;

            try
            {
                // make sure that we are not holding on to a file.
                DisconnectConnect();

                // Make sure that the target dir exists.
                System.IO.Directory.CreateDirectory(saveToPath);

                IPortableDeviceContent content = getContents();

                IPortableDeviceResources resources;
                content.Transfer(out resources);

                PortableDeviceApiLib.IStream wpdStream;
                uint optimalTransferSize = 0;

                var property = new _tagpropertykey();
                property.fmtid = new Guid(0xE81E79BE, 0x34F0, 0x41BF, 0xB5, 0x3F, 0xF1, 0xA0, 0x6A, 0xE8, 0x78, 0x42);
                property.pid   = 0;

                resources.GetStream(file.Id, ref property, 0, ref optimalTransferSize, out wpdStream);

                System.Runtime.InteropServices.ComTypes.IStream sourceStream = (System.Runtime.InteropServices.ComTypes.IStream)wpdStream;

                // var fileName = Path.GetFileName(file.Id);
                targetStream = new FileStream(Path.Combine(saveToPath, fileName), FileMode.Create, FileAccess.Write);

                // Getthe total size.
                long length  = file.size;
                long written = 0;
                long lPCt    = 0;
                unsafe
                {
                    var buffer = new byte[1024];
                    int bytesRead;
                    do
                    {
                        sourceStream.Read(buffer, 1024, new IntPtr(&bytesRead));
                        targetStream.Write(buffer, 0, bytesRead);

                        written += 1024;
                        long PCt = length > 0 ? (100 * written) / length : 100;
                        if (PCt != lPCt)
                        {
                            lPCt = PCt;
                            Console.WriteLine("Progress: " + lPCt);
                        }
                    } while (bytesRead > 0);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw ex;
            }
            finally
            {
                if (null != targetStream)
                {
                    targetStream.Close();
                }
                Disconnect();
            }
        }