Esempio n. 1
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);
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            CommandLine <CLArguments> commandLine = null;

            try
            {
                commandLine = new CommandLine <CLArguments>();
                var arguments = commandLine.Parse(args);

                using (var devices = WpdDeviceCollection.Create())
                {
                    if (devices.Count == 0)
                    {
                        Console.WriteLine("No devices found.");
                    }
                    else if (arguments.CommandDiscover)
                    {
                        foreach (var device in devices)
                        {
                            Console.WriteLine("\"{0}\"{1}", device.Name, arguments.ShowDeviceID ? " " + device.DeviceID : string.Empty);
                        }
                    }
                    else if (arguments.CommandInfo)
                    {
                        var device = devices.Find(arguments.InfoDeviceName, false);
                        if (device == null)
                        {
                            Console.WriteLine("No device found with the name \"{0}\".", arguments.InfoDeviceName);
                        }
                        else
                        {
                            if (arguments.InfoCopyID)
                            {
                                ClipboardApi.Copy(device.DeviceID, true);
                            }

                            Console.WriteLine("Found {0} properties.", device.Properties.Count);

                            for (var i = 0; i < device.Properties.Count; i++)
                            {
                                Console.WriteLine("{0} {1} ({2}): {3}", i.ToString("D" + device.Properties.Count.CountDigits().ToString()), PortableDevicePKeys.FindKeyName(device.Properties[i].Key), device.Properties[i].Type, device.Properties[i].Value);
                            }
                        }
                    }
                    else if (arguments.CommandUpload)
                    {
                        var device = devices.Find(arguments.UploadDeviceName, false);
                        if (device == null)
                        {
                            Console.WriteLine("No device found with the name \"{0}\".", arguments.UploadDeviceName);
                        }
                        else
                        {
                            Console.WriteLine("Uploading...");

                            var components   = WildcardSearch.SplitPattern(arguments.UploadSourcePath);
                            var targetObject = device.ObjectFromPath(arguments.UploadTargetPath, arguments.CreatePath);

                            var commander = new DeviceCommander(device);
                            commander.DataCopyStarted += (sender, e) =>
                            {
                                Console.WriteLine("\r{0}", e.SourcePath);
                            };
                            commander.DataCopied += (sender, e) =>
                            {
                                var percent = 100.0 * ((double)e.CopiedBytes / (double)e.MaxBytes);
                                Console.Write("\r  {0}/{1} bytes ({2}%)", e.CopiedBytes, e.MaxBytes, percent.ToString("G3"));
                            };
                            commander.DataCopyEnded += (sender, e) =>
                            {
                            };
                            commander.DataCopyError += (sender, e) =>
                            {
                                Console.WriteLine("\r" + e.Exception.Message);
                            };

                            Console.WriteLine();
                            if (String.IsNullOrEmpty(components.Item2))
                            {
                                commander.Upload(targetObject, components.Item1, arguments.Overwrite);
                            }
                            else
                            {
                                commander.Upload(targetObject, components.Item1, arguments.Overwrite, components.Item2, arguments.Recursive, arguments.Flatten);
                            }
                            Console.WriteLine("\rCompleted                                                                     ");
                        }
                    }
                    else if (arguments.CommandDownload)
                    {
                        var device = devices.Find(arguments.DownloadDeviceName, false);
                        if (device == null)
                        {
                            Console.WriteLine("No device found with the name \"{0}\".", arguments.DownloadDeviceName);
                        }
                        else
                        {
                            Console.WriteLine("Downloading...");

                            var components   = WildcardSearch.SplitPattern(arguments.DownloadSourcePath);
                            var sourceObject = device.ObjectFromPath(components.Item1, false);

                            if (!System.IO.Directory.Exists(arguments.DownloadTargetPath))
                            {
                                if (arguments.CreatePath)
                                {
                                    System.IO.Directory.CreateDirectory(arguments.DownloadTargetPath);
                                }
                                else
                                {
                                    throw new System.IO.DirectoryNotFoundException(String.Format("The directory \"{0}\" was not found.", arguments.DownloadTargetPath));
                                }
                            }

                            var commander = new DeviceCommander(device);
                            commander.DataCopyStarted += (sender, e) =>
                            {
                                Console.WriteLine("\r{0}", e.SourcePath);
                            };
                            commander.DataCopied += (sender, e) =>
                            {
                                var percent = 100.0 * ((double)e.CopiedBytes / (double)e.MaxBytes);
                                Console.Write("\r  {0}/{1} bytes ({2}%)", e.CopiedBytes, e.MaxBytes, percent.ToString("G3"));
                            };
                            commander.DataCopyEnded += (sender, e) =>
                            {
                            };
                            commander.DataCopyError += (sender, e) =>
                            {
                                Console.WriteLine("\r" + e.Exception.Message);
                            };

                            Console.WriteLine();
                            if (String.IsNullOrEmpty(components.Item2))
                            {
                                commander.Download(sourceObject, arguments.DownloadTargetPath, arguments.Overwrite);
                            }
                            else
                            {
                                commander.Download(sourceObject, arguments.DownloadTargetPath, arguments.Overwrite, components.Item2, arguments.Recursive, arguments.Flatten);
                            }
                            Console.WriteLine("\rCompleted                                                                     ");
                        }
                    }
                    else
                    {
                        Console.WriteLine(commandLine.Help());
                    }
                }
            }
            catch (CommandLineDeclarationException ex)
            {
                if (commandLine != null)
                {
                    Console.WriteLine(commandLine.Help());
                }
                System.Diagnostics.Trace.WriteLine(ex);
                Console.WriteLine(ex);
            }
            catch (CommandLineException ex)
            {
                if (commandLine != null)
                {
                    Console.WriteLine(commandLine.Help());
                }
                System.Diagnostics.Trace.WriteLine(ex);
                Console.WriteLine(ex);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex);
                Console.WriteLine(ex);
            }
        }