コード例 #1
0
        public unsafe byte[] DownloadFileToStream(PortableDeviceFile file)
        {
            IPortableDeviceContent iportableDeviceContent;

            PortableDeviceClass.Content(out iportableDeviceContent);
            IPortableDeviceResources iportableDeviceResources;

            iportableDeviceContent.Transfer(out iportableDeviceResources);
            uint            num = 0;
            _tagpropertykey tagpropertykey;

            tagpropertykey.fmtid = new Guid(3894311358U, 13552, 16831, 181, 63, 241, 160, 106, 232, 120, 66);
            tagpropertykey.pid   = 0;
            PortableDeviceApiLib.IStream istream;
            iportableDeviceResources.GetStream(file.Id, ref tagpropertykey, 0U, ref num, out istream);
            var stream = (System.Runtime.InteropServices.ComTypes.IStream)istream;

            using (var memoryStream = new MemoryStream())
            {
                var count    = 0;
                var cb       = 8192;
                var numArray = new byte[cb];
                do
                {
                    stream.Read(numArray, cb, new IntPtr(&count));
                    memoryStream.Write(numArray, 0, count);
                }while (count >= cb);
                Marshal.ReleaseComObject(stream);
                Marshal.ReleaseComObject(istream);
                return(memoryStream.ToArray());
            }
        }
コード例 #2
0
        public void DownloadFile(PortableDeviceFile file, string saveToPath)
        {
            PortableDeviceClass.Content(out IPortableDeviceContent content);
            content.Transfer(out IPortableDeviceResources resources);
            uint optimalTransferSize = 0;
            var  property            = new _tagpropertykey
            {
                fmtid = new Guid(0xE81E79BE, 0x34F0, 0x41BF, 0xB5, 0x3F, 0xF1, 0xA0, 0x6A, 0xE8, 0x78, 0x42),
                pid   = 0
            };

            resources.GetStream(file.Id, ref property, 0, ref optimalTransferSize, out PortableDeviceApiLib.IStream wpdStream);
            var sourceStream = (System.Runtime.InteropServices.ComTypes.IStream)wpdStream;
            var filename     = Path.GetFileName(file.Name);
            var targetStream = new FileStream(Path.Combine(saveToPath, filename), FileMode.Create, FileAccess.Write);

            unsafe
            {
                var buffer = new byte[1024];
                int bytesRead;
                do
                {
                    sourceStream.Read(buffer, 1024, new IntPtr(&bytesRead));
                    targetStream.Write(buffer, 0, 1024);
                } while (bytesRead > 0);
                targetStream.Close();
            }
        }
コード例 #3
0
        public string DownloadFileToString(PortableDeviceFile file)
        {
            PortableDeviceClass.Content(out IPortableDeviceContent content);
            content.Transfer(out IPortableDeviceResources resources);
            uint optimalTransferSize = 0;
            var  property            = new _tagpropertykey
            {
                fmtid = new Guid(0xE81E79BE, 0x34F0, 0x41BF, 0xB5, 0x3F, 0xF1, 0xA0, 0x6A, 0xE8, 0x78, 0x42),
                pid   = 0
            };

            resources.GetStream(file.Id, ref property, 0, ref optimalTransferSize, out PortableDeviceApiLib.IStream wpdStream);
            var sourceStream = (System.Runtime.InteropServices.ComTypes.IStream)wpdStream;
            var targetStream = new MemoryStream();

            unsafe
            {
                var buffer = new byte[1024];
                int bytesRead;
                do
                {
                    sourceStream.Read(buffer, 1024, new IntPtr(&bytesRead));
                    targetStream.Write(buffer, 0, 1024);
                } while (bytesRead > 0);
                //targetStream.Close();
            }
            var reader = new StreamReader(targetStream);
            var text   = reader.ReadToEnd();

            targetStream.Close();
            return(text);
        }
コード例 #4
0
ファイル: PortableDevice.cs プロジェクト: grzwolf/cfw
        private IPortableDeviceValues GetRequiredPropertiesForCopyWPD(PortableDeviceFile file, string parentObjectId)
        {
            IPortableDeviceValues values = new PortableDeviceTypesLib.PortableDeviceValues() as IPortableDeviceValues;

            _tagpropertykey WPD_OBJECT_PARENT_ID = new _tagpropertykey();

            WPD_OBJECT_PARENT_ID.fmtid = new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC, 0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
            WPD_OBJECT_PARENT_ID.pid   = 3;
            values.SetStringValue(ref WPD_OBJECT_PARENT_ID, parentObjectId);

            long            fileSize        = this.GetFileSize(file.Id);
            _tagpropertykey WPD_OBJECT_SIZE = new _tagpropertykey();

            WPD_OBJECT_SIZE.fmtid = new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC, 0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
            WPD_OBJECT_SIZE.pid   = 11;
            values.SetUnsignedLargeIntegerValue(WPD_OBJECT_SIZE, (ulong)fileSize);

            _tagpropertykey WPD_OBJECT_ORIGINAL_FILE_NAME = new _tagpropertykey();

            WPD_OBJECT_ORIGINAL_FILE_NAME.fmtid = new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC, 0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
            WPD_OBJECT_ORIGINAL_FILE_NAME.pid   = 12;
            values.SetStringValue(WPD_OBJECT_ORIGINAL_FILE_NAME, file.Name);

            _tagpropertykey WPD_OBJECT_NAME = new _tagpropertykey();

            WPD_OBJECT_NAME.fmtid = new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC, 0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
            WPD_OBJECT_NAME.pid   = 4;
            values.SetStringValue(WPD_OBJECT_NAME, file.Name);

            return(values);
        }
コード例 #5
0
        public void DeleteFile(PortableDevice device, String name)
        {
            PortableDeviceFile fileToDelete = null;

            try
            {
                foreach (var file in Files)
                {
                    if (file is PortableDeviceFile && name.Equals(file.Name))
                    {
                        fileToDelete = (PortableDeviceFile)file;
                        break;
                    }
                }

                // Got file?
                if (null != fileToDelete)
                {
                    device.DisconnectConnect();
                    device.DeleteFile(fileToDelete);
                    Files.Remove(fileToDelete);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                device.Disconnect();
            }
        }
コード例 #6
0
        /**
         * Delete a file.
         */
        public void DeleteFile(PortableDeviceFile file)
        {
            try
            {
                // make sure that we are not holding on to a file.
                DisconnectConnect();

                IPortableDeviceContent content = getContents();

                var variant = new PortableDeviceApiLib.tag_inner_PROPVARIANT();
                StringToPropVariant(file.Id, out variant);

                PortableDeviceApiLib.IPortableDevicePropVariantCollection objectIds =
                    new PortableDeviceTypesLib.PortableDevicePropVariantCollection()
                    as PortableDeviceApiLib.IPortableDevicePropVariantCollection;
                objectIds.Add(variant);

                content.Delete(0, objectIds, null);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw (ex);
            }
            Disconnect();
        }
コード例 #7
0
        public PortableDeviceFile FindFile(String fileName)
        {
            // First look for file in dir passed in.
            PortableDeviceFile result = null;

            if (null == result)
            {
                // Now look in subdirs.
                try
                {
                    foreach (var sub_data in Files)
                    {
                        if (sub_data.Name.Equals(fileName))
                        {
                            result = (PortableDeviceFile)sub_data;
                            break;
                        }
                        else
                        {
                            String matchStr = matching(sub_data.Name, fileName);
                            if (matchStr.Length > 0)
                            {
                                result = (PortableDeviceFile)sub_data;
                                break;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            return(result);
        }
コード例 #8
0
        public void DeleteFile(PortableDeviceFile file)
        {
            PortableDeviceClass.Content(out IPortableDeviceContent content);
            PortableDeviceApiLib.tag_inner_PROPVARIANT variant;
            StringToPropVariant(file.Id, out variant);
            var objectIds =
                new PortableDevicePropVariantCollection()
                as PortableDeviceApiLib.IPortableDevicePropVariantCollection;

            objectIds.Add(variant);
            content.Delete(0, objectIds, null);
        }
コード例 #9
0
        public void DeleteFile(PortableDeviceFile file)
        {
            IPortableDeviceContent content;

            this._device.Content(out content);

            var variant = new PortableDeviceApiLib.tag_inner_PROPVARIANT();

            StringToPropVariant(file.Id, out variant);

            PortableDeviceApiLib.IPortableDevicePropVariantCollection objectIds =
                new PortableDeviceTypesLib.PortableDevicePropVariantCollection()
                as PortableDeviceApiLib.IPortableDevicePropVariantCollection;
            objectIds.Add(variant);

            content.Delete(0, objectIds, null);
        }
コード例 #10
0
ファイル: PortableDevice.cs プロジェクト: grzwolf/cfw
        // DOWNLOAD file
        public bool DownloadFileFromWPD(PortableDeviceFile file, string saveToPath)
        {
            bool success = false;
            IPortableDeviceContent content;

            this._device.Content(out content);
            IPortableDeviceResources resources;

            content.Transfer(out resources);
            PortableDeviceApiLib.IStream wpdStream;
            uint            optimalTransferSize = 0;
            _tagpropertykey 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;
            string filename = Path.GetFileName(file.Name);

            System.IO.Directory.CreateDirectory(saveToPath);
            FileStream targetStream = new FileStream(Path.Combine(saveToPath, filename), FileMode.Create, FileAccess.Write);

            try {
                unsafe {
                    byte[] buffer = new byte[1024];
                    int    bytesRead;
                    do
                    {
                        sourceStream.Read(buffer, 1024, new IntPtr(&bytesRead));
                        //targetStream.Write(buffer, 0, 1024);
                        targetStream.Write(buffer, 0, bytesRead);
                    } while (bytesRead > 0);
                    targetStream.Close();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(sourceStream);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(wpdStream);
                }
                success = true;
            } catch {
                success = false;
            }
            return(success);
        }
コード例 #11
0
        /**
         * Copy test file to device.
         */
        public static String copyFromDevice(PortableDevice device)
        {
            String error = "";

            try
            {
                PortableDeviceFolder root   = device.Root;
                PortableDeviceObject result = root.FindDir(@"Phone\Android\data\test");
                if (null == result)
                {
                    // Perhaps it was a tablet instead of a phone?
                    result = root.FindDir(@"Tablet\Android\data\test");
                }

                // Did we find a the desired folder on the device?
                if (null == result)
                {
                    error = @"Dir Android\data not found!";
                }
                else if (result is PortableDeviceFolder)
                {
                    if (COPY_FOLDER)
                    {
                        // Copy a whole folder
                        ((PortableDeviceFolder)result).CopyFolderToPC(device, @"C:\Test\CopiedBackfromPhone");
                    }
                    else
                    {
                        // Or Copy a file
                        PortableDeviceFile file = ((PortableDeviceFolder)result).FindFile("foo.txt");
                        device.TransferContentFromDevice(file, @"C:\Test\CopiedBackfromPhone", "Copyfoo.txt");
                    }
                }
            }
            catch (Exception ex)
            {
                error = ex.Message;
            }

            return(error);
        }
コード例 #12
0
ファイル: PortableDevice.cs プロジェクト: grzwolf/cfw
        public bool CopyInsideWPD(PortableDeviceFile file, string destinationFolderId)
        {
            bool success = false;

            PortableDeviceApiLib.tag_inner_PROPVARIANT variant = new PortableDeviceApiLib.tag_inner_PROPVARIANT();
            StringToPropCopyVariant(file.Id, out variant);
            PortableDeviceApiLib.IPortableDevicePropVariantCollection objectIds = new PortableDeviceTypesLib.PortableDevicePropVariantCollection() as PortableDeviceApiLib.IPortableDevicePropVariantCollection;
            objectIds.Add(variant);

            IPortableDeviceContent content;

            this._device.Content(out content);
            try {
                PortableDeviceApiLib.IPortableDevicePropVariantCollection res = new PortableDeviceTypesLib.PortableDevicePropVariantCollection() as PortableDeviceApiLib.IPortableDevicePropVariantCollection;
                content.Copy(objectIds, destinationFolderId, ref res);
                success = true;
            } catch (Exception ex) {
                MessageBox.Show("CopyInsideWPD(..) - " + ex.Message);
                success = false;
            }
            return(success);
        }
コード例 #13
0
ファイル: PortableDevice.cs プロジェクト: grzwolf/cfw
        public bool DeleteFile(PortableDeviceFile file)
        {
            bool success = false;

            // original
            //            IPortableDeviceContent content;
            //            this._device.Content(out content);
            PortableDeviceApiLib.tag_inner_PROPVARIANT variant = new PortableDeviceApiLib.tag_inner_PROPVARIANT();
            StringToPropVariant(file.Id, out variant);
            PortableDeviceApiLib.IPortableDevicePropVariantCollection objectIds = new PortableDeviceTypesLib.PortableDevicePropVariantCollection() as PortableDeviceApiLib.IPortableDevicePropVariantCollection;
            objectIds.Add(variant);
            // the next 2 line need to appear AFTER "StringToPropVariant(file.Id, out variant);" - otherwise content is reset to null
            IPortableDeviceContent content;

            this._device.Content(out content);
            try {
                content.Delete(0, objectIds, null);
                success = true;
            } catch {
                success = false;
            }
            return(success);
        }
コード例 #14
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();
            }
        }
コード例 #15
0
ファイル: PortableDevice.cs プロジェクト: grzwolf/cfw
        // COPY inside of the same WPD
        public bool CopyInsideWPD2(PortableDeviceFile file, string parentObjectId)
        {
            bool success = false;

            // WPD source
            IPortableDeviceContent contentSrc;

            this._device.Content(out contentSrc);
            IPortableDeviceResources resourcesSrc;

            contentSrc.Transfer(out resourcesSrc);
            PortableDeviceApiLib.IStream wpdStreamSrc;
            uint            optimalTransferSizeSrc = 0;
            _tagpropertykey propertySrc            = new _tagpropertykey();

            propertySrc.fmtid = new Guid(0xE81E79BE, 0x34F0, 0x41BF, 0xB5, 0x3F, 0xF1, 0xA0, 0x6A, 0xE8, 0x78, 0x42);
            propertySrc.pid   = 0;
            resourcesSrc.GetStream(file.Id, ref propertySrc, 0, ref optimalTransferSizeSrc, out wpdStreamSrc);
            System.Runtime.InteropServices.ComTypes.IStream streamSrc = (System.Runtime.InteropServices.ComTypes.IStream)wpdStreamSrc;

            // WPD destination
            IPortableDeviceContent contentDst;

            this._device.Content(out contentDst);
            IPortableDeviceValues values = this.GetRequiredPropertiesForCopyWPD(file, parentObjectId);

            PortableDeviceApiLib.IStream tempStream;
            uint optimalTransferSizeBytes = 0;

            contentDst.CreateObjectWithPropertiesAndData(values, out tempStream, ref optimalTransferSizeBytes, null);
            System.Runtime.InteropServices.ComTypes.IStream streamDst = (System.Runtime.InteropServices.ComTypes.IStream)tempStream;

            try {
                unsafe {
                    byte[] buffer = new byte[1024];
                    int    bytesRead;
                    do
                    {
                        // read from source
                        streamSrc.Read(buffer, 1024, new IntPtr(&bytesRead));

                        // write to destination
                        IntPtr pcbWritten = IntPtr.Zero;
                        if (bytesRead > 0)
                        {
                            streamDst.Write(buffer, bytesRead, pcbWritten);
                        }
                    } while (bytesRead > 0);
                }
                success = true;
                streamDst.Commit(0);
            } catch (Exception) {
                success = false;
            } finally {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(tempStream);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(streamSrc);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(wpdStreamSrc);
            }

            return(success);
        }
コード例 #16
0
        public static void GetFile(PortableDeviceFile file, out StreamWrapper outputStream)
        {

            gopro.DownloadFile(file, out outputStream);

            //outputStream.Flush();
            //outputStream.Close();
        }
コード例 #17
0
        public void DeleteFile(PortableDeviceFile file)
        {
            IPortableDeviceContent content;
            this._device.Content(out content);

            var variant = new PortableDeviceApiLib.tag_inner_PROPVARIANT();
            StringToPropVariant(file.Id, out variant);

            PortableDeviceApiLib.IPortableDevicePropVariantCollection objectIds =
                new PortableDeviceTypesLib.PortableDevicePropVariantCollection()
                as PortableDeviceApiLib.IPortableDevicePropVariantCollection;
            objectIds.Add(variant);

            content.Delete(0, objectIds, null);
        }
コード例 #18
0
        public void DownloadFile(PortableDeviceFile file, string saveToPath)
        {
            IPortableDeviceContent content;
            this._device.Content(out content);

            IPortableDeviceResources resources;
            content.Transfer(out resources);

            PortableDeviceApiLib.IStream wpdStream = null;
            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;
            bool done = false;
            int numTries = 3;

            while(!done)
            {
                try
                {
                    resources.GetStream(file.Id, ref property, 0, ref optimalTransferSize, out wpdStream);
                    done = true;
                }
                catch (System.Runtime.InteropServices.COMException err)
                {
                    Console.WriteLine("-----------exception----------");
                    Console.WriteLine(err.GetType());
                    Console.WriteLine(err.Message);
                    Console.WriteLine(err.Source);
                    Console.WriteLine(err.TargetSite);
                    Console.WriteLine(err.ErrorCode);
                    Console.WriteLine(err.HelpLink);
                    Console.WriteLine("------------------------------");
                    System.Threading.Thread.Sleep(5000); //this codes make your application waiting for 5 seconds
                    numTries--;
                    resources.Cancel();
                    if (numTries < 0)
                        return;
                }
            }

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

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

            unsafe
            {
                int size = 1024;
                var buffer = new byte[size];
                int bytesRead;
                do
                {
                    sourceStream.Read(buffer, size, new IntPtr(&bytesRead));
                    targetStream.Write(buffer, 0, size);
                } while (bytesRead > 0);
                targetStream.Close();
                Marshal.ReleaseComObject(sourceStream);
                Marshal.ReleaseComObject(wpdStream);
            }
        }
コード例 #19
0
        public void DownloadFile(PortableDeviceFile file, out StreamWrapper sourceStream)
        {
            Connect();
            IPortableDeviceContent content = null;
            this._device.Content(out content);

            IPortableDeviceResources resources = null;
            content.Transfer(out resources);

            PortableDeviceApiLib.IStream wpdStream = null;
            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.Cancel();
            resources.GetStream(file.Id, ref property, 0, ref optimalTransferSize, out wpdStream);

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

            //sourceStream = ;
            //unsafe
            //{
            //    var buffer = new byte[1024];
            //    int bytesRead;
            //    do
            //    {
            //        sourceStream.Read(buffer, 1024, new IntPtr(&bytesRead));
            //        //outputStream.Write(buffer, 0, 1024);
            //    } while (bytesRead > 0);
            //    //targetStream.Close();                
            //}

            //System.Runtime.InteropServices.Marshal.ReleaseComObject(sourceStream);
            //System.Runtime.InteropServices.Marshal.ReleaseComObject(wpdStream);

            //sourceStream = null;
            //wpdStream = null;



        }
コード例 #20
0
        public void DownloadFile(PortableDeviceFile file, string saveToPath)
        {
            IPortableDeviceContent content;
            this._device.Content(out content);

            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);
            FileStream targetStream = new FileStream(Path.Combine(saveToPath, filename), FileMode.Create, FileAccess.Write);

            unsafe
            {
                var buffer = new byte[1024];
                int bytesRead;
                do
                {
                    sourceStream.Read(buffer, 1024, new IntPtr(&bytesRead));
                    targetStream.Write(buffer, 0, 1024);
                } while (bytesRead > 0);
                targetStream.Close();
            }
        }