Пример #1
0
        /// <summary>
        /// Upload a file tree to a portable device.
        /// </summary>
        /// <param name="device">Device class.</param>
        /// <param name="source">The path to the source.</param>
        /// <param name="destination">The path to the destination.</param>
        /// <param name="recursive">Include subdirectories or not</param>
        /// <exception cref="System.IO.IOException">path is a file name.</exception>
        /// <exception cref="System.ArgumentException">path is a zero-length string, contains only white space, or contains invalid characters as defined by System.IO.Path.GetInvalidPathChars.</exception>
        /// <exception cref="System.ArgumentNullException">path is null.</exception>
        /// <exception cref="System.IO.DirectoryNotFoundException">path is invalid.</exception>
        /// <exception cref="MediaDevices.NotConnectedException">device is not connected.</exception>
        public static void UploadFolder(this MediaDevice device, string source, string destination, bool recursive = true)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            if (!MediaDevice.IsPath(source))
            {
                throw new ArgumentException("source");
            }
            if (destination == null)
            {
                throw new ArgumentNullException("destination");
            }
            if (!MediaDevice.IsPath(destination))
            {
                throw new ArgumentException("destination");
            }
            if (!device.IsConnected)
            {
                throw new NotConnectedException("Not connected");
            }

            device.CreateDirectory(destination);

            if (recursive)
            {
                DirectoryInfo di = new DirectoryInfo(source);
                foreach (var e in di.EnumerateFileSystemInfos("*", SearchOption.AllDirectories))
                {
                    string path = Path.Combine(destination, e.FullName.Remove(0, source.Length + 1));
                    if (e.Attributes.HasFlag(FileAttributes.Directory))
                    {
                        device.CreateDirectory(path);
                    }
                    else
                    {
                        FileInfo fi = e as FileInfo;
                        using (FileStream stream = fi.OpenRead())
                        {
                            device.UploadFile(stream, path);
                        }
                    }
                }
            }
            else
            {
                DirectoryInfo di = new DirectoryInfo(source);
                foreach (FileInfo fi in di.EnumerateFiles())
                {
                    string path = Path.Combine(destination, fi.FullName.Remove(0, source.Length + 1));
                    using (FileStream stream = fi.OpenRead())
                    {
                        device.UploadFile(stream, path);
                    }
                }
            }
        }
        public Task CopyDesktopFileToDevice(string sourceDesktopFilePath, string destinationDeviceFilePath)
        {
            return(Task.Run(() =>
            {
                Monitor.Enter(this);
                try
                {
                    LOGGER.Debug("Copying {0} to device...", sourceDesktopFilePath);
                    destinationDeviceFilePath = GetAbsoluteDevicePath(destinationDeviceFilePath);
                    device.CreateDirectory(Path.GetDirectoryName(destinationDeviceFilePath));

                    if (device.FileExists(destinationDeviceFilePath))
                    {
                        device.DeleteFile(destinationDeviceFilePath);
                    }

                    device.UploadFile(sourceDesktopFilePath, destinationDeviceFilePath);
                    LOGGER.Trace("Finished copying {0}", sourceDesktopFilePath);
                }
                finally
                {
                    Monitor.Exit(this);
                }
            }));
        }
Пример #3
0
 private static int RunCommand(PutOption opts)
 {
     using (MediaDevice device = MediaDevice.GetDevices().FirstOrDefault(opts))
     {
         if (device != null)
         {
             device.Connect();
             string destDir = Path.GetDirectoryName(opts.Destination);
             if (!device.DirectoryExists(destDir))
             {
                 device.CreateDirectory(destDir);
             }
             device.UploadFile(opts.Source, opts.Destination);
             device.Disconnect();
         }
         else
         {
             Console.WriteLine("No device connected.");
             return(1);
         }
     }
     return(0);
 }