Example #1
0
        private void EnumerateContents(PortableDeviceFolder parentFolder, PortableDevice parentDevice, ref IPortableDeviceContent content, PortableDeviceFolder parent)
        {
            // Get the properties of the object
            IPortableDeviceProperties properties;

            content.Properties(out properties);

            // Enumerate the items contained by the current object
            IEnumPortableDeviceObjectIDs objectIds;

            content.EnumObjects(0, parent.Id, null, out objectIds);

            uint fetched = 0;

            do
            {
                fetched = 0;

                string objectId;

                objectIds.Next(1, out objectId, ref fetched);
                if (fetched > 0)
                {
                    var currentObject = WrapObject(properties, objectId);

                    currentObject.content      = content;
                    currentObject.parentDevice = parentDevice;
                    currentObject.parentObject = parentFolder;
                    parent.Files.Add(currentObject);
                }
            } while (fetched > 0);
        }
        public void CopyFolderToPC(PortableDevice device, String pcPath)
        {
            String errors = "";

            // Go thru all the files on the Phone.
            foreach (var file in Files)
            {
                try
                {
                    if (file is PortableDeviceFile)
                    {
                        device.TransferContentFromDevice((PortableDeviceFile)file, pcPath, file.Name);
                    }
                    else
                    {
                        errors += @"Could not find file:" + file.Name;
                    }
                }
                catch (Exception ex)
                {
                    errors += " " + ex.Message + " ";
                }
            }

            // Got errors?
            if (!String.IsNullOrEmpty(errors))
            {
                throw new Exception(errors);
            }
        }
        public void DeleteFile(PortableDevice device, String name)
        {
            PortableDeviceFile fileToDelete = null;

            try
            {
                foreach (var file in Files)
                {
                    if (file is PortableDeviceFile && name.Equals(file.Name))
                    {
                        fileToDelete = (PortableDeviceFile)file;
                        break;
                    }
                }

                // Got file?
                if (null != fileToDelete)
                {
                    device.DisconnectConnect();
                    device.DeleteFile(fileToDelete);
                    Files.Remove(fileToDelete);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                device.Disconnect();
            }
        }
Example #4
0
 //----------------------------------------------------------------------------------------------
 public static void DisplayObject(PortableDevice device, PortableDeviceObject portableDeviceObject)
 {
     Console.WriteLine(portableDeviceObject.Name);
     if (portableDeviceObject is PortableDeviceFolder)
     {
         DisplayFolderContents(device, (PortableDeviceFolder) portableDeviceObject);
     }
 }
Example #5
0
 public static void DisplayObject(PortableDevice device, PortableDeviceObject portableDeviceObject)
 {
     Console.WriteLine("device: " + device.FriendlyName + "   " + portableDeviceObject.Name);
     if (portableDeviceObject is PortableDeviceFolder)
     {
         DisplayFolderContents(device, (PortableDeviceFolder)portableDeviceObject);
     }
 }
        public static void ConnectDevice()
        {
            var devices = new PortableDeviceCollection();
            devices.Refresh();

            gopro = devices.First();
            gopro.Connect();
            currentDeviceId = gopro.DeviceId;
        }
Example #7
0
        /**
         * Copy test file to device.
         */
        public static String copyToDevice(PortableDevice device)
        {
            String error = "";

            try
            {
                // Try to find the data folder on the phone.
                String phoneDir             = @"Phone\Android\data";
                PortableDeviceFolder root   = device.Root;
                PortableDeviceFolder result = root.FindDir(phoneDir);
                if (null == result)
                {
                    // Perhaps it was a tablet instead of a phone?
                    result   = device.Root.FindDir(@"Tablet\Android\data");
                    phoneDir = @"Tablet\Android\data";
                }

                // Did we find a the desired folder on the device?
                if (null == result)
                {
                    error = phoneDir + " not found!";
                }
                else
                {
                    // Create remote test folder.
                    result = result.CreateDir(device, "test");

                    string pcDir = @"C:\Test\";

                    if (COPY_FOLDER)
                    {
                        // copy a whole folder. public void CopyFolderToPhone (PortableDevice device, String folderPath, String destPhonePath)
                        result.CopyFolderToPhone(device, pcDir, phoneDir);
                    }
                    else
                    {
                        // Or Copy a single file.
                        device.TransferContentToDevice(result, pcDir + "foo.txt");
                    }
                }
            }
            catch (Exception ex)
            {
                error = ex.Message;
            }

            return(error);
        }
Example #8
0
File: Program.cs Project: keyou/WPD
        public static void DisplayFolderContents(PortableDevice device, PortableDeviceFolder folder)
        {
            foreach (var item in folder.Files)
            {
                Console.WriteLine(item.Id);

                if (item is PortableDeviceFolder)
                {
                    DisplayFolderContents(device, (PortableDeviceFolder) item);
                }
                if (item is PortableDeviceFile)
                {
                    device.DownloadFile((PortableDeviceFile)item, @"c:\kindle\");
                }
            }
        }
Example #9
0
        public static void DisplayFolderContents(PortableDevice device, PortableDeviceFolder folder)
        {
            foreach (var item in folder.Files)
            {
                Console.WriteLine(item.Id);

                if (item is PortableDeviceFolder)
                {
                    DisplayFolderContents(device, (PortableDeviceFolder)item);
                }
                if (item is PortableDeviceFile)
                {
                    device.DownloadFile((PortableDeviceFile)item, @"c:\kindle\");
                }
            }
        }
Example #10
0
        private void LoadFiles()
        {
            PortableDeviceManager deviceManager = new PortableDeviceManager();

            deviceManager.RefreshDeviceList();
            uint numberOfDevices = 1;

            deviceManager.GetDevices(null, ref numberOfDevices);
            string[] deviceIds;
            string   temp1 = "";

            deviceIds = new string[numberOfDevices];
            deviceManager.GetDevices(ref temp1, ref numberOfDevices);
            PortableDevices.PortableDevice Drive = null;
            try
            {
                if (temp1 != "")
                {
                    PortableDevices.PortableDevice Device = new PortableDevices.PortableDevice(temp1);
                    try
                    {
                        Device.Connect();
                        string temp = Device.FriendlyName;
                        PortableDeviceFolder root   = Device.GetContents();
                        PortableDeviceFolder folder = root.Files.First() as PortableDeviceFolder;
                        foreach (string file in Directory.EnumerateFiles("Files"))
                        {
                            Device.TransferContentToDeviceFromStream(Path.GetFileName(file), new MemoryStream(File.ReadAllBytes(file)), folder.Id);
                        }
                    }
                    catch (Exception ex)
                    {
                        Device.Disconnect();
                        MessageBox.Show("Unable to load files Has this one already been loaded?");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Hit Exception:" + ex.ToString());
            }
            MessageBox.Show("Offload Complete! please unplug drive");
        }
Example #11
0
        private static void EnumerateContents(PortableDevice parentDevice, ref IPortableDeviceContent content, PortableDeviceFolder parent)
        {
            // Get the properties of the object
            IPortableDeviceProperties properties;

            content.Properties(out properties);

            // Enumerate the items contained by the current object
            IEnumPortableDeviceObjectIDs objectIds;

            content.EnumObjects(0, parent.Id, null, out objectIds);

            uint fetched = 0;

            do
            {
                fetched = 0;

                string objectId;

                objectIds.Next(1, out objectId, ref fetched);
                if (fetched > 0)
                {
                    var currentObject = WrapObject(properties, objectId);
                    currentObject.content      = content;
                    currentObject.parentDevice = parentDevice;

                    parent.Files.Add(currentObject);

                    if (currentObject is PortableDeviceFolder)
                    {
                        System.Diagnostics.Debugger.Log(0, "cat", "Opening: " + currentObject.Name + "\r\n");

                        //EnumerateContents(ref content, (PortableDeviceFolder) currentObject);
                    }
                    else
                    {
                        System.Diagnostics.Debugger.Log(0, "cat", "Adding: " + currentObject.Name + "\r\n");
                    }
                }
            } while (fetched > 0);
        }
Example #12
0
        public void RefreshContent()
        {
            PortableDevice parentDevice = this.parentDevice;

            this.Files.Clear();


            if (this.parentDevice != null)
            {
                this.parentDevice.Connect();
            }

            EnumerateContents(this, parentDevice, ref this.content, this);


            if (this.parentDevice != null)
            {
                this.parentDevice.Disconnect();
            }
        }
Example #13
0
        /**
         * Copy test file to device.
         */
        public static String copyFromDevice(PortableDevice device)
        {
            String error = "";

            try
            {
                PortableDeviceFolder root   = device.Root;
                PortableDeviceObject result = root.FindDir(@"Phone\Android\data\test");
                if (null == result)
                {
                    // Perhaps it was a tablet instead of a phone?
                    result = root.FindDir(@"Tablet\Android\data\test");
                }

                // Did we find a the desired folder on the device?
                if (null == result)
                {
                    error = @"Dir Android\data not found!";
                }
                else if (result is PortableDeviceFolder)
                {
                    if (COPY_FOLDER)
                    {
                        // Copy a whole folder
                        ((PortableDeviceFolder)result).CopyFolderToPC(device, @"C:\Test\CopiedBackfromPhone");
                    }
                    else
                    {
                        // Or Copy a file
                        PortableDeviceFile file = ((PortableDeviceFolder)result).FindFile("foo.txt");
                        device.TransferContentFromDevice(file, @"C:\Test\CopiedBackfromPhone", "Copyfoo.txt");
                    }
                }
            }
            catch (Exception ex)
            {
                error = ex.Message;
            }

            return(error);
        }
        /**
         * Device must be connected!
         *
         * Returns the id of the new folder.
         */
        public PortableDeviceFolder CreateDir(PortableDevice device, string folderName)
        {
            PortableDeviceFolder result = null;

            try
            {
                String newFolderId = "";

                // Already got folderName?
                result = FindDir(folderName);
                if (null == result)
                {
                    IPortableDeviceContent content = device.getContents();

                    // Get the properties of the object
                    IPortableDeviceProperties properties;
                    content.Properties(out properties);

                    IPortableDeviceValues createFolderValues = GetRequiredCreateDirPropertiesForContentType(Id, folderName);

                    content.CreateObjectWithPropertiesOnly(createFolderValues, ref newFolderId);

                    // Last, add to list of files.
                    result = new PortableDeviceFolder(newFolderId, folderName);

                    this.Files.Add(result);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                result = null;
            }

            return(result);
        }
        /**
         * CopyFolderToPhone
         */
        public void CopyFolderToPhone(PortableDevice device, String pcPath, String phonePath)
        {
            String errors = "";

            // Get all the files in the dir.
            string[] files = Directory.GetFiles(pcPath, "*.*", SearchOption.TopDirectoryOnly);
            foreach (var fn in files)
            {
                try
                {
                    device.TransferContentToDevice(this, fn);
                }
                catch (Exception ex)
                {
                    errors += @"\n\n" + ex.Message;
                }
            }

            // Got errors?
            if (!String.IsNullOrEmpty(errors))
            {
                throw new Exception(errors);
            }
        }
Example #16
0
 //---------------------------------------------------------------------------------------------------------
 public static void DisplayFolderContents(PortableDevice device, PortableDeviceFolder folder)
 {
     foreach (var item in folder.Files)
     {
         //Console.WriteLine("item: " + item);
         if (item is PortableDeviceFolder)
         {
             //Console.WriteLine("Folder: " + item.Id);
             DisplayFolderContents(device, (PortableDeviceFolder)item);
         }
         if (item is PortableDeviceFile)
         {
             if (!Directory.Exists(outputDir))
             {
                 Directory.CreateDirectory(outputDir);
             }
             Console.WriteLine("file: " + item.Name);
             //device.DownloadFile((PortableDeviceFile)item, @"K:\testCam\");
             device.DownloadFile((PortableDeviceFile)item, outputDir);
             //once its downloaded, delete the copy on the camnera
             device.DeleteFile((PortableDeviceFile)item);
         }
     }
 }