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));
        }
Exemple #2
0
        private bool TryGet(string deviceName, out PortableDevice portableDevice)
        {
            if (string.IsNullOrEmpty(deviceName))
            {
                portableDevice = null;
                return(false);
            }

            _deviceManager.RefreshDeviceList();

            // Determine how many WPD devices are connected
            var  deviceIds = new string[1];
            uint count     = 1;

            _deviceManager.GetDevices(ref deviceIds[0], ref count);

            // Retrieve the device id for each connected device
            deviceIds = new string[count];
            _deviceManager.GetDevices(ref deviceIds[0], ref count);
            foreach (var deviceId in deviceIds)
            {
                portableDevice = new PortableDevice(deviceId);
                portableDevice.Connect();
                if (portableDevice.Name == deviceName)
                {
                    return(true);
                }
            }
            portableDevice = null;
            return(false);
        }
Exemple #3
0
        public IEnumerable <string> GetTopLevel(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException(nameof(path));
            }
            if (!Exists(path))
            {
                throw new ArgumentException("Folder does not exist.", nameof(path));
            }

            PortableDevice mtpDevice = GetMtpDevice(path);

            if (mtpDevice == null)
            {
                return(null);
            }

            var mtpFolder = GetMtpFolder(mtpDevice, path);

            if (mtpFolder == null)
            {
                throw new IOException("Folder does not exist.");
            }
            return(mtpFolder.GetChildren(mtpDevice).Where(p => p is PortableDeviceFolder).Select(f => $"{path}{Path.DirectorySeparatorChar}{f.Name}"));
        }
        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);
        }
        public void Delete(PortableDevice mtpDevice, bool contentsAlso)
        {
            if (mtpDevice == null || !mtpDevice.IsConnected)
            {
                throw new ArgumentNullException(nameof(mtpDevice));
            }
            foreach (var child in GetChildren(mtpDevice))
            {
                if (contentsAlso)
                {
                    ((PortableDeviceFolder)child).Delete(mtpDevice, true);
                }
                else
                {
                    throw new IOException();
                }
            }

            tag_inner_PROPVARIANT variant;

            StringToPropVariant(Id, out variant);

            IPortableDevicePropVariantCollection objectIds =
                new PortableDevicePropVariantCollection()
                as IPortableDevicePropVariantCollection;

            objectIds.Add(variant);

            mtpDevice.Content.Delete(0, objectIds, null);
        }
Exemple #6
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);
            }
        }
Exemple #7
0
        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);
        }
Exemple #8
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}");
             }
         }
     }
 }
Exemple #9
0
        public void Delete(PortableDevice portableDevice)
        {
            if (portableDevice == null || !portableDevice.IsConnected)
            {
                throw new ArgumentNullException(nameof(portableDevice));
            }

            var variant = new tag_inner_PROPVARIANT();

            StringToPropVariant(Id, out variant);

            IPortableDevicePropVariantCollection objectIds =
                new PortableDevicePropVariantCollection()
                as IPortableDevicePropVariantCollection;

            objectIds.Add(variant);

            portableDevice.Content.Delete(0, objectIds, null);
        }
Exemple #10
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);
         }
     }
 }
        public IEnumerable <PortableDeviceObject> GetChildren(PortableDevice portableDevice, string filterPath = "")
        {
            if (portableDevice == null || !portableDevice.IsConnected)
            {
                throw new ArgumentNullException(nameof(portableDevice));
            }
            IPortableDeviceProperties properties;

            portableDevice.Content.Properties(out properties);

            IEnumPortableDeviceObjectIDs objectIds;

            portableDevice.Content.EnumObjects(0, Id, null, out objectIds);
            uint fetched = 0;

            do
            {
                string objectId;

                objectIds.Next(1, out objectId, ref fetched);
                if (fetched > 0)
                {
                    var currentObject = WrapObject(properties, objectId);
                    if (string.IsNullOrEmpty(filterPath) || currentObject.Name.ToLower() == Path.GetFileName(filterPath).ToLower())
                    {
                        if (currentObject is PortableDeviceFolder)
                        {
                            yield return(currentObject as PortableDeviceFolder);
                        }
                        else if (currentObject is PortableDeviceFile)
                        {
                            yield return(currentObject as PortableDeviceFile);
                        }
                    }
                }
            } while (fetched > 0);
        }