public void DownloadFile(PortableDeviceFile file, string saveToFolderPath) { 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(saveToFolderPath, 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(); } Marshal.ReleaseComObject(sourceStream); Marshal.ReleaseComObject(wpdStream); }
public void DeleteFile(PortableDeviceFile file) { IPortableDeviceContent content; this._device.Content(out content); var variant = new tag_inner_PROPVARIANT(); StringToPropVariant(file.Id, out variant); IPortableDevicePropVariantCollection objectIds = new PortableDeviceTypesLib.PortableDevicePropVariantCollection() as IPortableDevicePropVariantCollection; objectIds.Add(variant); content.Delete(0, objectIds, null); }