Example #1
0
        public void Delete(IWpdObject deleteObject)
        {
            var variant   = PROPVARIANT.Create_tag_inner_PROPVARIANT(deleteObject.ObjectID);
            var objectIds = (IPortableDevicePropVariantCollection) new PortableDeviceTypesLib.PortableDevicePropVariantCollection();

            objectIds.Add(variant);

            IPortableDevicePropVariantCollection results = null;

            deleteObject.Content.Delete(0, objectIds, ref results);
        }
Example #2
0
        public void Download(IWpdObject sourceObject, string targetDirectoryPath, bool overwrite, string searchPattern, bool recursive, bool flatten)
        {
            if (!sourceObject.IsContainer)
            {
                this.Download(sourceObject, targetDirectoryPath, overwrite);
                return;
            }

            var sourceDirectoryPath = sourceObject.GetPath();
            var sourceFilePaths     = sourceObject.GetChildPaths(searchPattern, recursive);

            sourceDirectoryPath = sourceDirectoryPath.EnsureLastCharacter('\\', true);

            string targetDirectory = null;

            foreach (var sourceFilePath in sourceFilePaths)
            {
                var sourceFileObject = this.device.ObjectFromPath(sourceFilePath, true);

                if (flatten)
                {
                    targetDirectory = targetDirectoryPath;
                }
                else
                {
                    var slash = sourceFilePath.LastIndexOf('\\');

                    if (slash < 0)
                    {
                        continue;
                    }

                    if (slash != sourceDirectoryPath.Length - 1)
                    {
                        var extra = sourceFilePath.Substring(sourceDirectoryPath.Length, slash - sourceDirectoryPath.Length);

                        var targetDirectoryExtraPath = targetDirectoryPath + Path.DirectorySeparatorChar + extra;

                        if (!Directory.Exists(targetDirectoryExtraPath))
                        {
                            Directory.CreateDirectory(targetDirectoryExtraPath);
                        }

                        targetDirectory = targetDirectoryExtraPath;
                    }
                    else
                    {
                        targetDirectory = targetDirectoryPath;
                    }
                }

                this.Download(sourceFileObject, targetDirectory, overwrite);
            }
        }
Example #3
0
 public void Upload(IWpdObject containerObject, string[] sourceFilePaths, bool overwrite)
 {
     foreach (var sourceFilePath in sourceFilePaths)
     {
         try
         {
             this.Upload(containerObject, sourceFilePath, overwrite);
         }
         catch (Exception)
         {
             // Hide exception and continue copy operation.
         }
     }
 }
Example #4
0
      public WpdObject( string objectID, IWpdObject parent, IPortableDeviceContent content )
      {
         this.ObjectID = objectID;
         this.Parent = parent;
         this.Content = content;
         this.Properties = new WpdPropertyCollection( this.Content, this.ObjectID );
         this.Properties.Refresh();

         var name = this.Properties.Find<string>( PortableDevicePKeys.WPD_OBJECT_NAME, false );
         this.Name = name == null ? string.Empty : name.Value;

         var originalFileName = this.Properties.Find<string>( PortableDevicePKeys.WPD_OBJECT_ORIGINAL_FILE_NAME, false );
         this.OriginalFileName = originalFileName == null ? string.Empty : originalFileName.Value;

         var size = this.Properties.Find<ulong>( PortableDevicePKeys.WPD_OBJECT_SIZE, false );
         this.Size = size == null ? 0UL : size.Value;

         var value = this.Properties.Find<Guid>( PortableDevicePKeys.WPD_OBJECT_CONTENT_TYPE, false );
         this.IsContainer = value == null ? false : value.Value == PortableDeviceGuids.WPD_CONTENT_TYPE_FOLDER;
      }
Example #5
0
        public IWpdObject CreateDirectory(IWpdObject containerObject, string directoryName)
        {
            var values = new PortableDeviceTypesLib.PortableDeviceValues() as IPortableDeviceValues;

            values.SetGuidValue(ref PortableDevicePKeys.WPD_OBJECT_FORMAT, PortableDeviceGuids.WPD_OBJECT_FORMAT_PROPERTIES_ONLY);
            values.SetGuidValue(ref PortableDevicePKeys.WPD_OBJECT_CONTENT_TYPE, PortableDeviceGuids.WPD_CONTENT_TYPE_FOLDER);

            // Parent ID of the new object.
            values.SetStringValue(ref PortableDevicePKeys.WPD_OBJECT_PARENT_ID, containerObject.ObjectID);

            // The original file name of the object.
            values.SetStringValue(PortableDevicePKeys.WPD_OBJECT_ORIGINAL_FILE_NAME, directoryName);

            // The name of the object on the device.
            values.SetStringValue(PortableDevicePKeys.WPD_OBJECT_NAME, directoryName);

            var objectID = string.Empty;

            containerObject.Content.CreateObjectWithPropertiesOnly(values, ref objectID);

            return(new WpdObject(objectID, containerObject, containerObject.Content));
        }
Example #6
0
        public IWpdObject ObjectFromPath(string path, bool createPath)
        {
            var entries = path.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);

            var start = 0;

            if ((entries.Length > 0) && (String.Compare(entries[0], this.Name) == 0))
            {
                start = 1;
            }

            IWpdObject final    = this;
            var        found    = false;
            var        children = this.GetChildren();

            for (var i = start; i < entries.Length; i++)
            {
                found = false;
                foreach (var child in children)
                {
                    if (String.Compare(child.Name, entries[i], true) == 0)
                    {
                        found = true;
                        final = child;
                        if (i < entries.Length - 1)
                        {
                            children = child.GetChildren();
                        }
                        break;
                    }
                }

                if (!found)
                {
                    foreach (var child in children)
                    {
                        if (child.OriginalFileName != null)
                        {
                            if (String.Compare(child.OriginalFileName, entries[i], true) == 0)
                            {
                                found = true;
                                final = child;
                                if (i < entries.Length - 1)
                                {
                                    children = child.GetChildren();
                                }
                                break;
                            }
                        }
                    }

                    if (!found)
                    {
                        if (createPath)
                        {
                            var commander = new DeviceCommander(this);
                            final    = commander.CreateDirectory(final, entries[i]);
                            children = new IWpdObject[0];
                        }
                        else
                        {
                            var errorEntries = new string[i + 2];
                            errorEntries[0] = this.Name;
                            for (var j = 0; j <= i; j++)
                            {
                                errorEntries[j + 1] = entries[j];
                            }

                            var errorPath = errorEntries.DelimitedString("\\");
                            throw new FileNotFoundException(String.Format("An object with the path \"{0}\" was not found.", errorPath));
                        }
                    }
                }
            }

            return(final);
        }
Example #7
0
        public void Download(IWpdObject sourceObject, string targetDirectoryPath, bool overwrite)
        {
            if (sourceObject.IsContainer)
            {
                var children = sourceObject.GetChildren();
                this.Download(children, targetDirectoryPath, overwrite);
                return;
            }

            var targetFilePath = Path.Combine(targetDirectoryPath, sourceObject.OriginalFileName);
            var sourceFilePath = sourceObject.GetPath();

            if (this.DataCopyStarted != null)
            {
                this.DataCopyStarted(this, new DataCopyStartedArgs(sourceFilePath, targetFilePath));
            }

            IPortableDeviceResources resources;

            sourceObject.Content.Transfer(out resources);

            var     key = PortableDevicePKeys.WPD_RESOURCE_DEFAULT;
            var     optimalBufferSize = 0U;
            IStream sourceStream      = null;

            resources.GetStream(sourceObject.ObjectID, ref key, PortableDeviceResourceAccessModes.STGM_READ, ref optimalBufferSize, out sourceStream);

            try
            {
                using (var targetStream = new FileStream(targetFilePath, overwrite ? FileMode.Create : FileMode.CreateNew, FileAccess.Write))
                {
                    var buffer         = new byte[optimalBufferSize];
                    var bytesRead      = 0U;
                    var totalBytesRead = 0U;

                    do
                    {
                        sourceStream.RemoteRead(out buffer[0], optimalBufferSize, out bytesRead);
                        if (bytesRead > 0)
                        {
                            targetStream.Write(buffer, 0, (int)bytesRead);
                            totalBytesRead += bytesRead;

                            if (this.DataCopied != null)
                            {
                                this.DataCopied(this, new DataCopiedArgs(sourceFilePath, targetFilePath, sourceObject.Size, totalBytesRead, bytesRead));
                            }
                        }
                    }while (bytesRead > 0);

                    targetStream.Flush();
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(String.Format("Failed to download file \"{0}\": {1}", sourceObject.GetPath(), ex));

                if (this.DataCopyError != null)
                {
                    this.DataCopyError(this, new DataCopyErrorArgs(sourceObject.GetPath(), targetFilePath, ex));
                }

                throw ex;
            }
            finally
            {
                if (sourceStream != null)
                {
                    Marshal.ReleaseComObject(sourceStream);
                }
            }

            if (this.DataCopyEnded != null)
            {
                this.DataCopyEnded(this, new DataCopyEndedArgs(sourceFilePath, targetFilePath));
            }
        }
Example #8
0
        public void Upload(IWpdObject containerObject, string sourceFilePath, bool overwrite)
        {
            var fileInfo = new FileInfo(sourceFilePath);

            if ((fileInfo.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
            {
                this.Upload(containerObject, sourceFilePath, overwrite, "*", true, false);
                return;
            }

            var fileName = Path.GetFileName(sourceFilePath);
            var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(sourceFilePath);
            var targetPath = containerObject.GetPath() + Path.DirectorySeparatorChar + fileName;

            var children = containerObject.GetChildren();

            foreach (var child in children)
            {
                if ((String.Compare(child.OriginalFileName, fileName, true) == 0))
                {
                    if (overwrite)
                    {
                        this.Delete(child);
                        break;
                    }
                    else
                    {
                        var ex = new IOException(String.Format("A file with the path \"{0}\" already exists on the device.", targetPath));

                        if (this.DataCopyError != null)
                        {
                            this.DataCopyError(this, new DataCopyErrorArgs(sourceFilePath, targetPath, ex));
                        }

                        throw ex;
                    }
                }
            }

            if (this.DataCopyStarted != null)
            {
                this.DataCopyStarted(this, new DataCopyStartedArgs(sourceFilePath, targetPath));
            }

            var values = new PortableDeviceTypesLib.PortableDeviceValues() as IPortableDeviceValues;

            // Parent ID of the new object.
            values.SetStringValue(ref PortableDevicePKeys.WPD_OBJECT_PARENT_ID, containerObject.ObjectID);

            // Size in bytes of the new object.
            values.SetUnsignedLargeIntegerValue(PortableDevicePKeys.WPD_OBJECT_SIZE, (ulong)fileInfo.Length);

            // The original file name of the object.
            values.SetStringValue(PortableDevicePKeys.WPD_OBJECT_ORIGINAL_FILE_NAME, fileName);

            // The name of the object on the device.
            values.SetStringValue(PortableDevicePKeys.WPD_OBJECT_NAME, fileNameWithoutExtension);

            IStream targetStream             = null;
            var     optimalTransferSizeBytes = 0U;

            containerObject.Content.CreateObjectWithPropertiesAndData(values, out targetStream, ref optimalTransferSizeBytes, null);

            try
            {
                using (var sourceStream = new FileStream(sourceFilePath, FileMode.Open, FileAccess.Read))
                {
                    var buffer       = new byte[optimalTransferSizeBytes];
                    var bytesRead    = 0;
                    var totalWritten = 0UL;

                    do
                    {
                        bytesRead = sourceStream.Read(buffer, 0, (int)optimalTransferSizeBytes);
                        if (bytesRead > 0)
                        {
                            var written = 0U;
                            targetStream.RemoteWrite(ref buffer[0], (uint)bytesRead, out written);
                            totalWritten += (ulong)written;

                            if (this.DataCopied != null)
                            {
                                this.DataCopied(this, new DataCopiedArgs(sourceFilePath, targetPath, (ulong)fileInfo.Length, totalWritten, (ulong)written));
                            }
                        }
                    }while (bytesRead > 0);
                }
                targetStream.Commit(0);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(String.Format("Failed to upload file \"{0}\": {1}", sourceFilePath, ex));

                if (this.DataCopyError != null)
                {
                    this.DataCopyError(this, new DataCopyErrorArgs(sourceFilePath, targetPath, ex));
                }
            }
            finally
            {
                if (targetStream != null)
                {
                    Marshal.ReleaseComObject(targetStream);
                }
            }

            if (this.DataCopyEnded != null)
            {
                this.DataCopyEnded(this, new DataCopyEndedArgs(sourceFilePath, targetPath));
            }
        }
Example #9
0
        public void Upload(IWpdObject containerObject, string sourceDirectoryPath, bool overwrite, string searchPattern, bool recursive, bool flatten)
        {
            var fileInfo = new FileInfo(sourceDirectoryPath);

            if ((fileInfo.Attributes & FileAttributes.Directory) != FileAttributes.Directory)
            {
                throw new InvalidOperationException("Source path must be a directory.");
            }

            if (String.IsNullOrEmpty(searchPattern))
            {
                var directoryName = Path.GetFileName(sourceDirectoryPath);

                var        found           = false;
                var        children        = containerObject.GetChildren();
                IWpdObject directoryObject = null;

                foreach (var child in children)
                {
                    if (String.Compare(child.Name, directoryName, true) == 0)
                    {
                        found           = true;
                        directoryObject = child;
                        break;
                    }
                }

                if (found)
                {
                    if (!overwrite)
                    {
                        throw new IOException(String.Format("The directory \"{0}\" already exists.", directoryObject.GetPath()));
                    }

                    containerObject = directoryObject;
                }
                else
                {
                    containerObject = this.CreateDirectory(containerObject, directoryName);
                }
            }

            var sourceFilePaths = Directory.GetFiles(sourceDirectoryPath,
                                                     String.IsNullOrEmpty(searchPattern) ? "*" : searchPattern,
                                                     recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);

            sourceDirectoryPath = sourceDirectoryPath.EnsureLastCharacter('\\', true);

            IWpdObject targetObject = null;

            foreach (var sourceFilePath in sourceFilePaths)
            {
                if (flatten)
                {
                    targetObject = containerObject;
                }
                else
                {
                    var slash = sourceFilePath.LastIndexOf('\\');

                    if (slash < 0)
                    {
                        continue;
                    }

                    if (slash != sourceDirectoryPath.Length - 1)
                    {
                        var extra = sourceFilePath.Substring(sourceDirectoryPath.Length, slash - sourceDirectoryPath.Length);

                        var targetObjectPath = containerObject.GetPath() + Path.DirectorySeparatorChar + extra;
                        targetObject = this.device.ObjectFromPath(targetObjectPath, true);
                    }
                    else
                    {
                        targetObject = containerObject;
                    }
                }

                this.Upload(targetObject, sourceFilePath, overwrite);
            }
        }