public unsafe void DownloadFile(PortableDeviceFile file, string saveToPath) { try { IPortableDeviceContent content; this._device.Content(out content); IPortableDeviceResources resources; content.Transfer(out resources); PortableDeviceApiLib.IStream wpdStream; 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 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(); } Marshal.ReleaseComObject(sourceStream); Marshal.ReleaseComObject(wpdStream); } catch (Exception ex) { throw new Exception(ex.Message); } }
public void DeleteFile(PortableDeviceFile file) { try { 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); } catch (Exception ex) { throw new Exception(ex.Message); } }