private static void EnumerateContents(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 { string objectId; objectIds.Next(1, out objectId, ref fetched); if (fetched > 0) { var currentObject = WrapObject(properties, objectId); parent.Files.Add(currentObject); if (currentObject is PortableDeviceFolder) { EnumerateContents(ref content, (PortableDeviceFolder)currentObject); } } } while (fetched > 0); }
public PortableDeviceFolder GetContents() { var root = new PortableDeviceFolder("DEVICE", "DEVICE"); PortableDeviceClass.Content(out IPortableDeviceContent content); EnumerateContents(ref content, root); return(root); }
public void refreshFolderContents(PortableDeviceFolder parent) { IPortableDeviceContent content; this._device.Content(out content); parent.Files.Clear(); EnumerateContents(ref content, parent, -10); }
public PortableDeviceFolder GetContents() { var root = new PortableDeviceFolder("DEVICE", "DEVICE"); IPortableDeviceContent content; this._device.Content(out content); EnumerateContents(ref content, root, 0); return(root); }
/** * The root folder on the device, also enumerate all files and folers on device. */ public PortableDeviceFolder Root() { PortableDeviceFolder root = new PortableDeviceFolder("DEVICE", "DEVICE"); IPortableDeviceContent content = getContents(); EnumerateContents(ref content, root); return(root); }
public static void DisplayFolderContents(PortableDeviceFolder folder) { foreach (var item in folder.Files) { Console.WriteLine(item.Id); if (item is PortableDeviceFolder) { DisplayFolderContents((PortableDeviceFolder)item); } } }
public static void DisplayFolderContents(PortableDeviceFolder folder) { foreach (var item in folder.Files) { Console.WriteLine(item.Id); if (item is PortableDeviceFolder) { DisplayFolderContents((PortableDeviceFolder) item); } } }
public PortableDeviceFolder GetContents() { var root = new PortableDeviceFolder("DEVICE", "DEVICE"); IPortableDeviceContent content; this._device.Content(out content); EnumerateContents(ref content, root); return root; }
private static void GetFiles(ref IPortableDeviceContent content, PortableDeviceFolder folder) { PortableDeviceFolder.EnumerateContents(ref content, folder); foreach (var fileItem in folder.Files) { Console.WriteLine($"\t{fileItem.Name}"); if (fileItem is PortableDeviceFolder childFolder) { GetFiles(ref content, childFolder); } } }
/** * Connect to device */ public void Connect() { if (this._isConnected) { return; } var clientInfo = (IPortableDeviceValues) new PortableDeviceTypesLib.PortableDeviceValues(); this._device.Open(this.DeviceId, clientInfo); _root = new PortableDeviceFolder("DEVICE", "DEVICE"); this._isConnected = true; }
public List <wpdFileInfo> GetFolderContentList(string targetFolder, ref string targetFolderID) { // it always starts from the device' root, ensures to be up to date string[] arr = targetFolder.Split('\\'); string rootFolder = arr[0]; PortableDeviceFolder root = new PortableDeviceFolder("DEVICE", rootFolder); List <wpdFileInfo> retlist = new List <wpdFileInfo>(); targetFolderID = ""; IPortableDeviceContent content; this._device.Content(out content); EnumerateFolderContent(ref content, root, rootFolder, targetFolder, ref retlist, ref targetFolderID); return(retlist); }
/** * 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); }
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\"); } } }
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\"); } } }
public PortableDeviceFolder GetContents() { var root = new PortableDeviceFolder("DEVICE", "DEVICE"); IPortableDeviceContent content; this._device.Content(out content); root.parentDevice = this; root.content = content; root.RefreshContent(); //EnumerateContents(this, ref content, root); return(root); }
public PortableDeviceFolder GetContents(PortableDeviceFolder folder, bool isRecurse) { EnumerateContents(ref this._content, ref this._properties, folder); if (isRecurse) { foreach (PortableDeviceObject obj in folder.Files) { if (obj is PortableDeviceFolder) { this.GetContents((PortableDeviceFolder)obj, isRecurse); } } } return(folder); }
public void TransferContentToDevice(string fileName, PortableDeviceFolder parentFolder) { string parentObjectId = parentFolder.Id; IPortableDeviceContent content; this._device.Content(out content); IPortableDeviceValues values = GetRequiredPropertiesForContentType(fileName, parentObjectId); PortableDeviceApiLib.IStream tempStream; uint optimalTransferSizeBytes = 0; content.CreateObjectWithPropertiesAndData( values, out tempStream, ref optimalTransferSizeBytes, null); System.Runtime.InteropServices.ComTypes.IStream targetStream = (System.Runtime.InteropServices.ComTypes.IStream)tempStream; try { using (var sourceStream = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { var buffer = new byte[optimalTransferSizeBytes]; int bytesRead; do { bytesRead = sourceStream.Read( buffer, 0, (int)optimalTransferSizeBytes); IntPtr pcbWritten = IntPtr.Zero; targetStream.Write( buffer, bytesRead, pcbWritten); } while (bytesRead > 0); } targetStream.Commit(0); } finally { Marshal.ReleaseComObject(tempStream); } }
public void DeleteFolder(PortableDeviceFolder folder) { IPortableDeviceContent content; PortableDeviceClass.Content(out content); var variant = new PortableDeviceApiLib.tag_inner_PROPVARIANT(); StringToPropVariant(folder.Id, out variant); var objectIds = new PortableDevicePropVariantCollection() as PortableDeviceApiLib.IPortableDevicePropVariantCollection; objectIds.Add(variant); content.Delete(1, objectIds, null); }
/** * 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); }
public void EnumerateContents(ref IPortableDeviceContent content, PortableDeviceFolder parent, int level) { if (level < 2) { // 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 { string objectId; objectIds.Next(1, out objectId, ref fetched); if (fetched > 0) { var currentObject = WrapObject(properties, objectId); parent.Files.Add(currentObject); if (currentObject is PortableDeviceFolder) { if (!currentObject.Name.Equals("ServicePlatform")) { EnumerateContents(ref content, (PortableDeviceFolder)currentObject, level + 1); } else { ServicePlatformFolder = currentObject; EnumerateContents(ref content, (PortableDeviceFolder)currentObject, 0); } } } } while (fetched > 0); } }
static void Main() { var devices = new PortableDeviceCollection(); devices.Refresh(); var kindle = devices.First(); kindle.Connect(); var root = kindle.GetContents(); foreach (var resource in root.Files) { DisplayResourceContents(resource); } PortableDeviceFolder documents = (from r in ((PortableDeviceFolder)root.Files[0]).Files where r.Name.Contains("documents") select r).First() as PortableDeviceFolder; var books = from b in documents.Files where b.Name.Contains("Kindle_Users_Guide") select b; foreach (PortableDeviceFile book in books) { Console.WriteLine(book.Id); kindle.DeleteFile(book); } kindle.Disconnect(); Console.WriteLine(); Console.WriteLine("Press any key to continue."); Console.ReadKey(); }
static void Main(string[] args) { var devices = new PortableDeviceCollection(); devices.Refresh(); var device = devices.First(); device.Connect(); PortableDeviceFolder folder = device.GetContents(); Console.WriteLine(folder.Name); foreach (PortableDeviceObject obj in folder.Files) { Console.WriteLine(obj.Name); } device.Disconnect(); Console.WriteLine(); Console.WriteLine("Press any key to continue."); Console.ReadKey(); }
/** * FindDir - look in dir passed in and also in any subdirs. */ public PortableDeviceFolder FindDir(String path) { PortableDeviceFolder result = null; try { foreach (var word in path.Split('\\')) { if (Name.Equals(word)) { result = (PortableDeviceFolder)this; break; } else { foreach (var sub_data in Files) { if (sub_data is PortableDeviceFolder) { if (sub_data.Name.Equals(word)) { String subPath = last(path, word); result = ((PortableDeviceFolder)sub_data).FindDir(subPath); break; } } } } } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } return(result); }
/** * 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); }
//--------------------------------------------------------------------------------------------------------- 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); } } }
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); }
public PortableDeviceFolder GetRootContent(bool isRecurse) { var root = new PortableDeviceFolder("DEVICE", "DEVICE", 0); return(this.GetContents(root, isRecurse)); }
private static void EnumerateFolderContent(ref IPortableDeviceContent content, PortableDeviceFolder parent, string currentFolder, string targetFolder, ref List <wpdFileInfo> retlist, ref string targetFolderID) { // save ID of the parent when target folder is reached if (targetFolder == currentFolder) { targetFolderID = parent.Id; } // 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 { string objectId; objectIds.Next(1, out objectId, ref fetched); if (fetched > 0) { PortableDeviceObject currentObject = WrapObject(properties, objectId); // only collect objects within the targetFolder, ignore all other object if (targetFolder == currentFolder) { // build item string name = currentObject.Name; if (currentObject is PortableDeviceFolder) { // get file modified date IPortableDeviceKeyCollection keys; properties.GetSupportedProperties(currentObject.Id, out keys); IPortableDeviceValues values; properties.GetValues(currentObject.Id, keys, out values); string objDateStr = ""; _tagpropertykey property = new _tagpropertykey(); property.fmtid = new Guid("EF6B490D-5CD8-437A-AFFC-DA8B60EE4A3C"); property.pid = 19; //WPD_OBJECT_DATE_MODIFIED try { values.GetStringValue(property, out objDateStr); // 2016/10/19:18:52:52.000 } catch {; } retlist.Add(new wpdFileInfo("p:" + name, "wpd_" + currentObject.Id, 0, objDateStr)); } if (currentObject is PortableDeviceFile) { // get file properties IPortableDeviceKeyCollection keys; properties.GetSupportedProperties(currentObject.Id, out keys); IPortableDeviceValues values; properties.GetValues(currentObject.Id, keys, out values); // get file size long objSize; _tagpropertykey property = new _tagpropertykey(); property.fmtid = new Guid("EF6B490D-5CD8-437A-AFFC-DA8B60EE4A3C"); property.pid = 11; //WPD_OBJECT_SIZE; values.GetSignedLargeIntegerValue(property, out objSize); // get file modified date string objDateStr; property.pid = 19; //WPD_OBJECT_DATE_MODIFIED values.GetStringValue(property, out objDateStr); // 2016/10/19:18:52:52.000 //DateTime objDateUTC = (DateTime.ParseExact(objDateStr, "dd.MM.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AssumeLocal)).ToUniversalTime(); // 2016/10/19:18:52:52.000 // transfer file to return list retlist.Add(new wpdFileInfo("f:" + name, "wpd_" + currentObject.Id, objSize, objDateStr)); } } // a path sequence along the targetFolder is needed; as long as currentFolder is the beginning of the targetFolder, we are on the right track string latestFolder = Path.Combine(currentFolder, currentObject.Name); if ((targetFolder.StartsWith(latestFolder))) { // next level starts with a new currentFolder currentFolder = latestFolder; EnumerateFolderContent(ref content, (PortableDeviceFolder)currentObject, currentFolder, targetFolder, ref retlist, ref targetFolderID); // after returning from a deeper level, currentFolder needs to reset to the next higher level currentFolder = Path.GetDirectoryName(currentFolder); } } } while (fetched > 0); }
static void GetAllFilesFromFolder(PortableDeviceFolder folder) { foreach (var file in folder.Files) { if (file is PortableDeviceFile) files.Add(file as PortableDeviceFile); else if (file is PortableDeviceFolder) GetAllFilesFromFolder(file as PortableDeviceFolder); } }
private PortableDeviceFolder GetFolder(string name) { List <string> parts; PortableDeviceFolder result, tempFolder; this.Refresh(); result = null; parts = name.Split(new char[] { '\\', '/' }, System.StringSplitOptions.RemoveEmptyEntries).ToList(); result = GetCachedFile(name) as PortableDeviceFolder; if (result != null) { return(result); } if (parts.Count > 0) { foreach (PortableDevice d in this) { d.Connect(); if (d.FriendlyName.ToLower() == parts[0].ToLower()) { tempFolder = d.GetContents(); tempFolder.RefreshContent(); if (parts.Count == 1) { result = tempFolder; d.Disconnect(); break; } for (int i = 1; i < parts.Count; i++) { bool found = false; for (int k = 0; k < tempFolder.Files.Count; k++) { PortableDeviceFolder f = tempFolder.Files[k] as PortableDeviceFolder; if (f is PortableDeviceFolder == true) { if (parts[i].ToLower() == f.Name.ToLower()) { found = true; tempFolder = f; tempFolder.RefreshContent(); if (i == parts.Count - 1) { result = f as PortableDeviceFolder; if (recentFiles.ContainsKey(name) == false) { recentFiles.Add(name, f); } recentFiles[name] = f; } break; } } } if (found == false) { break; } } } d.Disconnect(); } } return(result); }
// https://cgeers.wordpress.com/2012/04/17/wpd-transfer-content-to-a-device/ static void Main() { string error = string.Empty; PortableDeviceCollection devices = null; try { devices = new PortableDeviceCollection(); if (null != devices) { char cmdCharacter = ' '; do { devices.Refresh(); Console.Clear(); Console.WriteLine($"Found {devices.Count} Device(s)"); Console.WriteLine($"-------------------------------------"); foreach (var device in devices) { Console.WriteLine(); Console.WriteLine($"Found Device {device.Name} with ID {device.DeviceId}"); Console.WriteLine($"\tManufacturer: {device.Manufacturer}"); Console.WriteLine($"\tDescription: {device.Description}"); device.Connect(); var rootfolder = device.Root; Console.WriteLine($"Root Folder {rootfolder.Name}"); IPortableDeviceContent content = device.getContents(); // list all contents in the root - 1 level in //see GetFiles method to enumerate everything in the device PortableDeviceFolder.EnumerateContents(ref content, rootfolder); foreach (var fileItem in rootfolder.Files) { Console.WriteLine($"\t{fileItem.Name}"); if (fileItem is PortableDeviceFolder childFolder) { PortableDeviceFolder.EnumerateContents(ref content, childFolder); foreach (var childFile in childFolder.Files) { Console.WriteLine($"\t\t{childFile.Name}"); } } } // Copy folder to device from pc. //error = copyToDevice (device); //if (String.IsNullOrEmpty(error)) //{ // error = @"Copied folder C:\Test to Phone\Android\data\test"; //} //Console.WriteLine(error); //// Copy folder back to pc from device. //error = copyFromDevice(device); //if (String.IsNullOrEmpty(error)) //{ // error = @"Copied folder Phone\Android\data\test to c:\Test\CopiedBackfromPhone"; //} device.Disconnect(); } Console.WriteLine($"-------------------------------------"); Console.WriteLine("Press r to refresh"); Console.WriteLine("Press x key to exit"); cmdCharacter = Console.ReadKey().KeyChar; if (cmdCharacter == 'x') { break; } } while ('r' == cmdCharacter); } } catch (Exception ex) { error = ex.Message; } finally { if (null != devices) { devices.Dispose(); } if (!string.IsNullOrWhiteSpace(error)) { Console.WriteLine(error); Console.ReadKey(); } } }
/** * Copy To Phone */ public void TransferContentToDevice(PortableDeviceFolder parentFolder, string filePath) { PortableDeviceApiLib.IStream tempStream = null; System.Runtime.InteropServices.ComTypes.IStream targetStream = null; try { string fileName = PortableDeviceFolder.last(filePath, "\\"); // Remove existing remote file, if it exists. parentFolder.DeleteFile(this, fileName); // make sure that we are not holding on to a file. DisconnectConnect(); string parentObjectId = parentFolder.Id; IPortableDeviceContent content = getContents(); IPortableDeviceValues values = GetRequiredPropertiesForContentType(filePath, parentObjectId); uint optimalTransferSizeBytes = 0; content.CreateObjectWithPropertiesAndData( values, out tempStream, ref optimalTransferSizeBytes, null); targetStream = (System.Runtime.InteropServices.ComTypes.IStream)tempStream; long length = new System.IO.FileInfo(filePath).Length; long written = 0; long lPCt = 0; using (var sourceStream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { var buffer = new byte[optimalTransferSizeBytes]; int bytesRead; do { bytesRead = sourceStream.Read(buffer, 0, (int)optimalTransferSizeBytes); IntPtr PCbWritten = IntPtr.Zero; targetStream.Write(buffer, bytesRead, PCbWritten); written += bytesRead; long PCt = length > 0 ? (100 * written) / length : 100; if (PCt != lPCt) { lPCt = PCt; Console.WriteLine("Progress: " + lPCt); } } while (bytesRead > 0); } targetStream.Commit(0); } catch (Exception ex) { Console.WriteLine(ex.Message); throw ex; } finally { if (null != targetStream) { Marshal.ReleaseComObject(targetStream); } if (null != tempStream) { Marshal.ReleaseComObject(tempStream); } Disconnect(); } }