public void CreateDirectory(string path) { if (string.IsNullOrEmpty(path)) { throw new ArgumentNullException(nameof(path)); } var mtpDevice = MtpDeviceManager.GetPortableDevice(path); var folderOnlyPath = path.Replace($"{MtpPathInterpreter.GetMtpDeviceName(path)}{Path.DirectorySeparatorChar}", string.Empty); string lastExistentFolderPath = folderOnlyPath; var mtpFolder = mtpDevice.GetLastExistentFolder(ref lastExistentFolderPath); if (lastExistentFolderPath == folderOnlyPath) { return; } if (mtpFolder == null || string.IsNullOrEmpty(lastExistentFolderPath)) { mtpDevice.Disconnect(); throw new IOException(); } var newMtpFolder = PortableDeviceFolder.Create(mtpDevice, mtpFolder, folderOnlyPath.Replace($"{lastExistentFolderPath}{Path.DirectorySeparatorChar}", String.Empty)); if (newMtpFolder == null) { mtpDevice.Disconnect(); throw new IOException(); } }
public bool Exists(string path) { if (string.IsNullOrEmpty(path)) { throw new ArgumentNullException(nameof(path)); } PortableDevice mtpDevice; try { mtpDevice = MtpDeviceManager.GetPortableDevice(path); } catch (Exception e) { LoggingManager.LogSciendoSystemError(e); return(false); } var mtpFile = mtpDevice.GetObject(path.Replace(MtpPathInterpreter.GetMtpDeviceName(path), string.Empty)) as PortableDeviceFile; if (mtpFile == null) { mtpDevice.Disconnect(); return(false); } return(true); }
public byte[] Read(string path) { if (string.IsNullOrEmpty(path)) { throw new ArgumentNullException(nameof(path)); } var mtpDevice = MtpDeviceManager.GetPortableDevice(path); if (!Exists(path)) { throw new ArgumentException("Path does not exist."); } var mtpFile = mtpDevice.GetObject(path.Replace(MtpPathInterpreter.GetMtpDeviceName(path), string.Empty)) as PortableDeviceFile; if (mtpFile == null) { mtpDevice.Disconnect(); throw new IOException("Path not accessible."); } IPortableDeviceResources resources; mtpDevice.Content.Transfer(out resources); PortableDeviceApiLib.IStream wpdStream; uint optimalTransferSize = 0; resources.GetStream(mtpFile.Id, ref PortableDevicePKeys.WPD_RESOURCE_DEFAULT, 0, ref optimalTransferSize, out wpdStream); System.Runtime.InteropServices.ComTypes.IStream sourceStream = (System.Runtime.InteropServices.ComTypes.IStream)wpdStream; MemoryStream targetStream = new MemoryStream(); unsafe { var buffer = new byte[1024]; int bytesRead; do { sourceStream.Read(buffer, 1024, new IntPtr(&bytesRead)); targetStream.Write(buffer, 0, bytesRead); } while (bytesRead > 0); } var len = targetStream.Length; byte[] result = new byte[len]; targetStream.Position = 0; targetStream.Read(result, 0, (int)len); targetStream.Close(); resources.Cancel(); mtpDevice.Disconnect(); return(result); }
private static PortableDeviceFolder GetMtpFolder(PortableDevice mtpDevice, string path) { if (mtpDevice == null) { return(null); } var mtpFolder = mtpDevice.GetObject(path.Replace(MtpPathInterpreter.GetMtpDeviceName(path), string.Empty)) as PortableDeviceFolder; if (mtpFolder == null) { mtpDevice.Disconnect(); } return(mtpFolder); }
public void Delete(string path, bool contentsAlso) { if (string.IsNullOrEmpty(path)) { throw new ArgumentNullException(nameof(path)); } var mtpDevice = MtpDeviceManager.GetPortableDevice(path); var mtpFolder = mtpDevice.GetObject(path.Replace(MtpPathInterpreter.GetMtpDeviceName(path), string.Empty)) as PortableDeviceFolder; if (mtpFolder == null) { mtpDevice.Disconnect(); return; } mtpFolder.Delete(mtpDevice, contentsAlso); }
public static PortableDevice GetPortableDevice(string path) { if (string.IsNullOrEmpty(path)) { throw new ArgumentNullException(nameof(path)); } if (!MtpPathInterpreter.IsMtpDevice(path)) { throw new IOException("Wrong path for this adapter."); } PortableDevice mtpDevice; if (!CreateInstance().TryGet(MtpPathInterpreter.GetMtpDeviceName(path), out mtpDevice)) { mtpDevice?.Disconnect(); throw new IOException(); } return(mtpDevice); }
public void Create(string path, byte[] body) { if (string.IsNullOrEmpty(path)) { throw new ArgumentNullException(nameof(path)); } var mtpDevice = MtpDeviceManager.GetPortableDevice(path); if (Exists(path)) { return; } new MtpDirectory().CreateDirectory(Path.GetDirectoryName(path)); var folderOnlyPath = Path.GetDirectoryName(path.Replace($"{MtpPathInterpreter.GetMtpDeviceName(path)}{Path.DirectorySeparatorChar}", string.Empty)); string lastExistentFolderPath = folderOnlyPath; var mtpFolder = mtpDevice.GetLastExistentFolder(ref lastExistentFolderPath); if (lastExistentFolderPath != folderOnlyPath) { throw new IOException(); } if (mtpFolder == null || string.IsNullOrEmpty(lastExistentFolderPath)) { mtpDevice.Disconnect(); throw new IOException(); } try { PortableDeviceFile.Create(mtpDevice, mtpFolder, Path.GetFileName(path), body); } catch (Exception) { mtpDevice.Disconnect(); throw; } }