Esempio n. 1
0
        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();
            }
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        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);
        }
Esempio n. 4
0
        private static PortableDevice GetMtpDevice(string path)
        {
            PortableDevice mtpDevice;

            try
            {
                mtpDevice = MtpDeviceManager.GetPortableDevice(path);
            }
            catch (Exception e)
            {
                LoggingManager.LogSciendoSystemError(e);
                return(null);
            }
            return(mtpDevice);
        }
Esempio n. 5
0
        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);
        }
Esempio n. 6
0
        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;
            }
        }