Пример #1
0
        public static PortableDeviceFolder Create(PortableDevice portableDevice, PortableDeviceFolder parentFolder, string folderName)
        {
            if (portableDevice == null || !portableDevice.IsConnected)
            {
                throw new ArgumentNullException(nameof(portableDevice));
            }
            if (parentFolder == null)
            {
                throw new ArgumentNullException(nameof(parentFolder));
            }
            if (string.IsNullOrEmpty(folderName))
            {
                throw new ArgumentNullException(nameof(folderName));
            }
            var folderNameParts = folderName.Split(new[] { Path.DirectorySeparatorChar },
                                                   StringSplitOptions.RemoveEmptyEntries);

            PortableDeviceFolder newDeviceFolder     = null;
            PortableDeviceFolder currentParentFolder = parentFolder;

            foreach (var folderNamePart in folderNameParts)
            {
                newDeviceFolder = CreateOneFolder(portableDevice, currentParentFolder, folderNamePart);
                if (newDeviceFolder == null)
                {
                    return(null);
                }
                currentParentFolder = newDeviceFolder;
            }
            return(newDeviceFolder);
        }
Пример #2
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();
            }
        }
Пример #3
0
        public static void Create(PortableDevice portableDevice,
                                  PortableDeviceFolder parentFolder,
                                  string fileName,
                                  byte[] body)
        {
            if (portableDevice == null || !portableDevice.IsConnected)
            {
                throw new ArgumentNullException(nameof(portableDevice));
            }
            if (string.IsNullOrEmpty(fileName))
            {
                throw new ArgumentNullException(nameof(fileName));
            }
            if (body == null)
            {
                throw new ArgumentNullException(nameof(body));
            }
            IPortableDeviceValues values =
                GetRequiredPropertiesForContentType(fileName, parentFolder.Id, (ulong)body.LongLength);

            PortableDeviceApiLib.IStream tempStream;
            uint optimalTransferSizeBytes = 0;

            portableDevice.Content.CreateObjectWithPropertiesAndData(
                values,
                out tempStream,
                ref optimalTransferSizeBytes,
                null);
            var targetStream = tempStream as IStream;

            try
            {
                using (var sourceStream = new MemoryStream(body))
                {
                    var buffer = new byte[body.Length];
                    if (sourceStream.Read(buffer, 0, body.Length) <= 0)
                    {
                        return;
                    }
                    IntPtr pcbWritten = IntPtr.Zero;
                    targetStream.Write(buffer, body.Length, pcbWritten);
                    targetStream.Commit(0);
                }
            }
            finally
            {
                Marshal.ReleaseComObject(tempStream);
            }
        }
Пример #4
0
        public void Connect()
        {
            if (IsConnected)
            {
                return;
            }

            var clientInfo = new PortableDeviceValuesClass() as IPortableDeviceValues;

            _device.Open(DeviceId, clientInfo);
            IsConnected = true;

            PortableDeviceRootFolder = new PortableDeviceFolder(_deviceStringId, _deviceStringId);

            _device.Content(out _content);
        }
Пример #5
0
 private IEnumerable <string> EnumerateFiles(PortableDevice mtpDevice,
                                             PortableDeviceFolder mtpFolder, string path, SearchOption searchOption)
 {
     foreach (var deviceObject in mtpFolder.GetChildren(mtpDevice))
     {
         if (deviceObject is PortableDeviceFile)
         {
             yield return($"{path}{Path.DirectorySeparatorChar}{deviceObject.Name}");
         }
         else if (deviceObject is PortableDeviceFolder && searchOption == SearchOption.AllDirectories)
         {
             foreach (var fileName in EnumerateFiles(mtpDevice, deviceObject as PortableDeviceFolder, $"{path}{Path.DirectorySeparatorChar}{deviceObject.Name}", SearchOption.AllDirectories))
             {
                 yield return($"{fileName}");
             }
         }
     }
 }
Пример #6
0
 private IEnumerable <string> GetContents(
     PortableDevice mtpDevice,
     PortableDeviceFolder mtpFolder,
     string path,
     SearchOption searchOption,
     string[] extensions = null)
 {
     if (extensions == null)
     {
         extensions = new[] { "*.*" }
     }
     ;
     foreach (var extension in extensions)
     {
         ExtensionsRead?.Invoke(this, new ExtensionsReadEventArgs(extension));
         foreach (var file in EnumerateFiles(mtpDevice, mtpFolder, path, searchOption)
                  .Where(s => s.ToLower().EndsWith(extension.ToLower()) || extension == "*.*"))
         {
             yield return(file);
         }
     }
 }
Пример #7
0
        private static PortableDeviceFolder CreateOneFolder(PortableDevice portableDevice, PortableDeviceFolder parentFolder,
                                                            string folderName)
        {
            if (portableDevice == null || !portableDevice.IsConnected)
            {
                throw new ArgumentNullException(nameof(portableDevice));
            }
            if (parentFolder == null)
            {
                throw new ArgumentNullException(nameof(parentFolder));
            }
            if (string.IsNullOrEmpty(folderName))
            {
                throw new ArgumentNullException(nameof(folderName));
            }

            IPortableDeviceValues values =
                GetRequiredPropertiesForContentType(folderName, parentFolder.Id);

            if (values == null)
            {
                throw new Exception($"Could not set values for creation of folder:{folderName}");
            }
            var newFolderId = parentFolder.Id;

            portableDevice.Content.CreateObjectWithPropertiesOnly(
                values, ref newFolderId);
            return(new PortableDeviceFolder(newFolderId, folderName));
        }