/// <summary> /// Transfer from device to computer /// Source : http://cgeers.com/2011/08/13/wpd-transferring-content/ /// Inspired by nikon-camera-control /// </summary> /// <param name="deviceObject"></param> /// <param name="targetStream"></param> public void Pull(PortableDeviceObject deviceObject, Stream targetStream) { IPortableDeviceContent content; portableDeviceClass.Content(out content); IPortableDeviceResources resources; content.Transfer(out resources); IStream wpdStream; uint optimalTransferSize = 0; _tagpropertykey property = PortableDevicePKeys.WPD_RESOURCE_DEFAULT; int numRetries = 3; const int retryTimeout = 500; const uint STGM_READ = 0; do { try { resources.GetStream(deviceObject.ID, ref property, STGM_READ, ref optimalTransferSize, out wpdStream); numRetries = 0; } catch (COMException comException) { if ((uint)comException.ErrorCode == PortableDeviceErrorCodes.ERROR_BUSY) { Thread.Sleep(retryTimeout); } throw; } } while (numRetries-- > 0); var sourceStream = (System.Runtime.InteropServices.ComTypes.IStream)wpdStream; unsafe { var buffer = new byte[1024 * 256]; int bytesRead; do { sourceStream.Read(buffer, buffer.Length, new IntPtr(&bytesRead)); if (bytesRead == 0) { break; } targetStream.Write(buffer, 0, bytesRead); } while (bytesRead > 0); targetStream.Close(); } Marshal.ReleaseComObject(sourceStream); Marshal.ReleaseComObject(wpdStream); }
/// <summary> /// This method loads the sub folders and files within the device. /// NOTE: It only loads the subfolder and file information. /// No actual binary is loaded as this could potentially be a very /// piece of data in memory. /// </summary> /// <param name="portableDeviceItem"></param> private void LoadDeviceData(PortableDeviceClass portableDeviceItem) { IPortableDeviceContent content; portableDeviceItem.Content(out content); LoadDeviceItems(content); }
public unsafe byte[] DownloadFileToStream(PortableDeviceFile file) { IPortableDeviceContent iportableDeviceContent; PortableDeviceClass.Content(out iportableDeviceContent); IPortableDeviceResources iportableDeviceResources; iportableDeviceContent.Transfer(out iportableDeviceResources); uint num = 0; _tagpropertykey tagpropertykey; tagpropertykey.fmtid = new Guid(3894311358U, 13552, 16831, 181, 63, 241, 160, 106, 232, 120, 66); tagpropertykey.pid = 0; PortableDeviceApiLib.IStream istream; iportableDeviceResources.GetStream(file.Id, ref tagpropertykey, 0U, ref num, out istream); var stream = (System.Runtime.InteropServices.ComTypes.IStream)istream; using (var memoryStream = new MemoryStream()) { var count = 0; var cb = 8192; var numArray = new byte[cb]; do { stream.Read(numArray, cb, new IntPtr(&count)); memoryStream.Write(numArray, 0, count); }while (count >= cb); Marshal.ReleaseComObject(stream); Marshal.ReleaseComObject(istream); return(memoryStream.ToArray()); } }
public PortableDeviceFolder GetContents() { var root = new PortableDeviceFolder("DEVICE", "DEVICE"); IPortableDeviceContent content; PortableDeviceClass.Content(out content); EnumerateContents(ref content, root); return(root); }
public void CreateFolder(string folderName, string parentObjectId) { IPortableDeviceContent content; PortableDeviceClass.Content(out content); string objectID = null; var values = GetRequiredPropertiesForFolderType(folderName, parentObjectId); content.CreateObjectWithPropertiesOnly(values, objectID); }
/// <summary> /// 获取设备信息 /// </summary> /// <param name="deviceId"></param> /// <returns></returns> private static IPortableDeviceContent GetDeviceContent(string deviceId) { IPortableDeviceValues clientInfo = (IPortableDeviceValues) new PortableDeviceTypesLib.PortableDeviceValuesClass(); PortableDeviceClass portableDevice = new PortableDeviceClass(); portableDevice.Open(deviceId, clientInfo); IPortableDeviceContent content; portableDevice.Content(out content); return(content); }
public EntryObjectEnumerator(Device device) { Reset(); _hEntryManager = new EntryManager(device); PortableDeviceClass deviceClass = new PortableDeviceClass(); deviceClass.Open(device.Id, DeviceManager.ClientValues); deviceClass.Content(out _hDeviceContent); _lEntryObjects = new List <EntryObject>(); FindChild(_sCurrentObjectId, _iCurrentLevel); }
public string CreateFolder(string folderName, string parentObjectId) { IPortableDeviceContent content; PortableDeviceClass.Content(out content); var values = GetRequiredPropertiesForFolder(folderName, parentObjectId); string folderId = null; content.CreateObjectWithPropertiesOnly(values, ref folderId); return(folderId); }
private void StartEnumerate(IObjectEnumerateHelper enumHelper) { lock (dispatcher) { enumerateHelper = enumHelper; IPortableDeviceContent pContent; PortableDeviceClass.Content(out pContent); Content = new PortableDeviceFunctionalObject("DEVICE"); Enumerate(ref pContent, "DEVICE", Content, enumerateHelper); RaisePropertyChanged("Content"); } }
public void Connect() { if (IsConnected) { return; } var clientInfo = new PortableDeviceValuesClass() as IPortableDeviceValues; _device.Open(DeviceId, clientInfo); IsConnected = true; PortableDeviceRootFolder = new PortableDeviceFolder(_deviceStringId, _deviceStringId); _device.Content(out _content); }
/// <summary> /// This method gets the list of properties from the properties list that pertain only to the device. /// </summary> /// <param name="portableDeviceItem"></param> /// <returns></returns> private IPortableDeviceValues ExtractDeviceProperties(PortableDeviceClass portableDeviceItem) { IPortableDeviceContent content; IPortableDeviceProperties properties; portableDeviceItem.Content(out content); content.Properties(out properties); // Retrieve the values for the properties IPortableDeviceValues propertyValues; properties.GetValues(Id, null, out propertyValues); return(propertyValues); }
public void TransferContentToDevice(string fileName, string parentObjectId) { IPortableDeviceContent content; PortableDeviceClass.Content(out content); var values = GetRequiredPropertiesForContentType(fileName, parentObjectId); PortableDeviceApiLib.IStream tempStream; uint optimalTransferSizeBytes = 0; content.CreateObjectWithPropertiesAndData( values, out tempStream, ref optimalTransferSizeBytes, null); var 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); var pcbWritten = IntPtr.Zero; targetStream.Write( buffer, bytesRead, pcbWritten); } while (bytesRead > 0); } targetStream.Commit(0); } finally { Marshal.ReleaseComObject(tempStream); } }
/// <summary> /// 连接设备 /// </summary> /// <param name="DeviceId"></param> /// <param name="portableDevice"></param> /// <param name="deviceContent"></param> /// <returns></returns> private static IPortableDeviceValues Connect(string DeviceId, out PortableDevice portableDevice, out IPortableDeviceContent deviceContent) { IPortableDeviceValues clientInfo = (IPortableDeviceValues) new PortableDeviceTypesLib.PortableDeviceValuesClass(); portableDevice = new PortableDeviceClass(); portableDevice.Open(DeviceId, clientInfo); portableDevice.Content(out deviceContent); IPortableDeviceProperties deviceProperties; deviceContent.Properties(out deviceProperties); IPortableDeviceValues deviceValues; deviceProperties.GetValues("DEVICE", null, out deviceValues); return(deviceValues); }
/** * Get the contents of the device. */ public IPortableDeviceContent getContents() { IPortableDeviceContent contents = null; try { Connect(); _device.Content(out contents); } catch (Exception ex) { Console.WriteLine(ex.Message); throw ex; } return(contents); }
public void DeleteFile(PortableDeviceFile file) { IPortableDeviceContent content; PortableDeviceClass.Content(out content); var variant = new PortableDeviceApiLib.tag_inner_PROPVARIANT(); StringToPropVariant(file.Id, out variant); var objectIds = new PortableDevicePropVariantCollection() as PortableDeviceApiLib.IPortableDevicePropVariantCollection; objectIds.Add(variant); content.Delete(0, objectIds, null); }
public void TransferContentToDeviceFromStream(string fileName, MemoryStream inputStream, string parentObjectId) { IPortableDeviceContent content; PortableDeviceClass.Content(out content); var values = GetRequiredPropertiesForContentTypeFromStream(inputStream, fileName, parentObjectId); PortableDeviceApiLib.IStream tempStream; uint optimalTransferSizeBytes = 0; content.CreateObjectWithPropertiesAndData( values, out tempStream, ref optimalTransferSizeBytes, null); var targetStream = (System.Runtime.InteropServices.ComTypes.IStream)tempStream; try { using (var memoryStream = inputStream) { var numArray = new byte[(int)optimalTransferSizeBytes]; int cb; do { cb = memoryStream.Read(numArray, 0, (int)optimalTransferSizeBytes); var zero = IntPtr.Zero; targetStream.Write(numArray, cb, zero); }while (cb > 0); } targetStream.Commit(0); } finally { Marshal.ReleaseComObject(tempStream); } }
public string DownloadFileToString(PortableDeviceFile file) { IPortableDeviceContent content; PortableDeviceClass.Content(out content); IPortableDeviceResources resources; content.Transfer(out resources); PortableDeviceApiLib.IStream wpdStream; uint optimalTransferSize = 0; var property = new _tagpropertykey { fmtid = new Guid(0xE81E79BE, 0x34F0, 0x41BF, 0xB5, 0x3F, 0xF1, 0xA0, 0x6A, 0xE8, 0x78, 0x42), pid = 0 }; resources.GetStream(file.Id, ref property, 0, ref optimalTransferSize, out wpdStream); var sourceStream = (System.Runtime.InteropServices.ComTypes.IStream)wpdStream; var targetStream = new MemoryStream(); unsafe { var buffer = new byte[1024]; int bytesRead; do { sourceStream.Read(buffer, 1024, new IntPtr(&bytesRead)); targetStream.Write(buffer, 0, 1024); } while (bytesRead > 0); //targetStream.Close(); } var reader = new StreamReader(targetStream); var text = reader.ReadToEnd(); targetStream.Close(); return(text); }
/// <summary> /// 写入数据 /// </summary> /// <param name="sourceFile"></param> /// <param name="portableDevice"></param> /// <param name="parentId"></param> private static void TransferContentToDevice(string sourceFile, PortableDeviceClass portableDevice, string parentId) { IPortableDeviceContent content; portableDevice.Content(out content); IPortableDeviceValues values = GetRequiredPropertiesForContentType(sourceFile, parentId); 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 (FileStream sourceStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read)) { int bytesRead = 0; do { int count = 1024 * 1024; byte[] buffer = new byte[count]; bytesRead = sourceStream.Read(buffer, 0, count); IntPtr pcbWritten = IntPtr.Zero; targetStream.Write(buffer, bytesRead, pcbWritten); }while (bytesRead > 0); } targetStream.Commit(0); } finally { Marshal.ReleaseComObject(tempStream); } }
public void CopyFilefromDevice(PortableDeviceFile file, string saveToPath) { unsafe { IPortableDeviceContent portableDeviceContent; IPortableDeviceResources portableDeviceResource; PortableDeviceApiLib.IStream stream; int num = 0; _device.Content(out portableDeviceContent); portableDeviceContent.Transfer(out portableDeviceResource); uint num1 = 0; PortableDeviceApiLib._tagpropertykey __tagpropertykey = new PortableDeviceApiLib._tagpropertykey() { fmtid = new Guid(-400655938, 13552, 16831, 181, 63, 241, 160, 106, 232, 120, 66), pid = 0 }; portableDeviceResource.GetStream(file.Id, ref __tagpropertykey, 0, ref num1, out stream); System.Runtime.InteropServices.ComTypes.IStream stream1 = (System.Runtime.InteropServices.ComTypes.IStream)stream; string str = (string.IsNullOrEmpty(file.Name) ? Path.GetFileName(file.Id) : Path.GetFileName(file.Name)); var fileStream = new FileStream(Path.Combine(saveToPath, str), FileMode.Create, FileAccess.Write); byte[] numArray = new byte[1024]; do { //stream1.Read(numArray, 1024, new IntPtr(ref num)); stream1.Read(numArray, 1024, new IntPtr(&num)); fileStream.Write(numArray, 0, 1024); }while (num > 0); fileStream.Close(); Marshal.ReleaseComObject(stream1); Marshal.ReleaseComObject(stream); } }
public void DownloadFile(PortableDeviceFile file, string saveToPath) { IPortableDeviceContent content; PortableDeviceClass.Content(out content); IPortableDeviceResources resources; content.Transfer(out resources); PortableDeviceApiLib.IStream wpdStream; uint optimalTransferSize = 0; var property = new _tagpropertykey { fmtid = new Guid(0xE81E79BE, 0x34F0, 0x41BF, 0xB5, 0x3F, 0xF1, 0xA0, 0x6A, 0xE8, 0x78, 0x42), pid = 0 }; resources.GetStream(file.Id, ref property, 0, ref optimalTransferSize, out wpdStream); var sourceStream = (System.Runtime.InteropServices.ComTypes.IStream)wpdStream; var filename = Path.GetFileName(file.Name); var targetStream = new FileStream(Path.Combine(saveToPath, filename), FileMode.Create, FileAccess.Write); unsafe { var buffer = new byte[1024]; int bytesRead; do { sourceStream.Read(buffer, 1024, new IntPtr(&bytesRead)); targetStream.Write(buffer, 0, 1024); } while (bytesRead > 0); targetStream.Close(); } }
public IEnumerable <TransFileObject> GetAllFiles(string ext, MainWindowData mainWnd) { if (srcDevId == null || srcDirObjId == null) { return(null); } var files = new List <TransFileObject>(); PortableDeviceClass device = new PortableDeviceClass(); try { IPortableDeviceContent content; IPortableDeviceProperties properties; var clientInfo = (IPortableDeviceValues) new PortableDeviceValuesClass(); device.Open(srcDevId, clientInfo); device.Content(out content); content.Properties(out properties); // コピー対象の拡張子と一致するファイルを抽出 var patExt = new Regex("." + ext, RegexOptions.Compiled); var srcfiles = GetObjects(srcDirObjId, content, TransFileObject.ObjectKind.FILE); foreach (var srcfile in srcfiles) { if (patExt.IsMatch(srcfile.fileName)) { files.Add(new TransFileObject(srcfile.fileName, srcfile.objId, srcfile.updateTime, srcfile.kind)); } } } catch (Exception e) { mainWnd.DispInfo = string.Format("エラーが発生しました\n{0}", e.Message); } finally { device.Close(); } return(files); }
public string[] ExecCopyFile(string destDirpath, IEnumerable <TransFileObject> copyFiles, MainWindowData mainWnd) { if (srcDevId == null || srcDirObjId == null) { return(null); } var filedfile = new List <string>(); int cnt = 0; int total = copyFiles.Count(); PortableDeviceClass device = new PortableDeviceClass(); IPortableDeviceContent content; var clientInfo = (IPortableDeviceValues) new PortableDeviceValuesClass(); device.Open(srcDevId, clientInfo); device.Content(out content); foreach (var file in copyFiles) { try { DownloadFile(file, destDirpath + file.fileName, content); mainWnd.DispInfo += String.Format("成功:{0}\n", file.fileName); cnt++; } catch (Exception e) { filedfile.Add(file.fileName); mainWnd.DispInfo += String.Format("失敗:{0} [{1}]\n", e.Message, file.fileName); } mainWnd.Progress = (100 * cnt) / total; } device.Close(); return(filedfile.ToArray()); }
/// <summary> /// This method loads the sub folders and files within the device. /// NOTE: It only loads the subfolder and file information. /// No actual binary is loaded as this could potentially be a very /// piece of data in memory. /// </summary> /// <param name="portableDeviceItem"></param> private void LoadDeviceData(PortableDeviceClass portableDeviceItem) { IPortableDeviceContent content; portableDeviceItem.Content(out content); LoadDeviceItems(content); }
/// <summary> /// This method gets the list of properties from the properties list that pertain only to the device. /// </summary> /// <param name="portableDeviceItem"></param> /// <returns></returns> private IPortableDeviceValues ExtractDeviceProperties(PortableDeviceClass portableDeviceItem) { IPortableDeviceContent content; IPortableDeviceProperties properties; portableDeviceItem.Content(out content); content.Properties(out properties); // Retrieve the values for the properties IPortableDeviceValues propertyValues; properties.GetValues(Id, null, out propertyValues); return propertyValues; }
public void BeginFetchContent() { _hDeviceClass.Content(out _hDeviceContent); EnumerateFetchContent("DEVICE", 0); }
public Hashtable GetDeviceProperties(string deviceId, string objectId) { Hashtable properties = new Hashtable(); PortableDeviceClass deviceClass = new PortableDeviceClass(); IPortableDeviceContent deviceContent; deviceClass.Open(deviceId, _hClientDeviceValues); deviceClass.Content(out deviceContent); IPortableDeviceProperties deviceProperties; deviceContent.Properties(out deviceProperties); IPortableDeviceValues deviceValues; deviceProperties.GetValues(objectId, null, out deviceValues); uint devicePropertyCount = 0; deviceValues.GetCount(ref devicePropertyCount); for (uint i = 0; i < devicePropertyCount; i++) { _tagpropertykey tagPropertyKey = new _tagpropertykey(); tag_inner_PROPVARIANT tagPropVariant = new tag_inner_PROPVARIANT(); deviceValues.GetAt(i, ref tagPropertyKey, ref tagPropVariant); IntPtr ptrValue = Marshal.AllocHGlobal(Marshal.SizeOf(tagPropVariant)); Marshal.StructureToPtr(tagPropVariant, ptrValue, false); PropVariant pvValue = (PropVariant)Marshal.PtrToStructure(ptrValue, typeof(PropVariant)); if (pvValue.variantType == VariantTypes.VT_LPWSTR) { string stringValue = Marshal.PtrToStringUni(pvValue.pointerValue); properties.Add(PropertyManager.GetKeyName(tagPropertyKey.pid, tagPropertyKey.fmtid), stringValue); } else if (pvValue.variantType == VariantTypes.VT_DATE) { DateTime datetime = DateTime.FromOADate(pvValue.dateValue); properties.Add(PropertyManager.GetKeyName(tagPropertyKey.pid, tagPropertyKey.fmtid), datetime); } else if (pvValue.variantType == VariantTypes.VT_UI4) { uint intValue = pvValue.byteValue; properties.Add(PropertyManager.GetKeyName(tagPropertyKey.pid, tagPropertyKey.fmtid), intValue); } else if (pvValue.variantType == VariantTypes.VT_UI8) { long intValue = pvValue.longValue; properties.Add(PropertyManager.GetKeyName(tagPropertyKey.pid, tagPropertyKey.fmtid), intValue); } else if (pvValue.variantType == VariantTypes.VT_UINT) { long intValue = pvValue.longValue; properties.Add(PropertyManager.GetKeyName(tagPropertyKey.pid, tagPropertyKey.fmtid), intValue); } } return(properties); }
public bool FindSrcDir(string volume, string dirpath, out string retryMsg, MainWindowData mainWnd) { const string defretryMsg = "機器を接続してOKを選択してください"; retryMsg = defretryMsg; // 接続中のデバイス数を取得 uint count = 0; deviceManager.RefreshDeviceList(); deviceManager.GetDevices(null, ref count); if (count == 0) { retryMsg = defretryMsg; return(false); } // コピー元デバイス、フォルダの存在チェック string[] deviceIds = new string[count]; PortableDeviceClass[] devices = new PortableDeviceClass[count]; deviceManager.GetDevices(deviceIds, ref count); var clientInfo = (IPortableDeviceValues) new PortableDeviceValuesClass(); bool existsSrcDir = true; foreach (var deviceId in deviceIds) { PortableDeviceClass device = new PortableDeviceClass(); try { // デバイス情報の取得 IPortableDeviceContent content; IPortableDeviceProperties properties; device.Open(deviceId, clientInfo); device.Content(out content); content.Properties(out properties); IPortableDeviceValues propertyValues; properties.GetValues("DEVICE", null, out propertyValues); var property = new _tagpropertykey(); string devicename; property.fmtid = new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC, 0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C); property.pid = 4; propertyValues.GetStringValue(property, out devicename); //Console.WriteLine(devicename); // 対象デバイスで無ければスキップ if (devicename != volume) { continue; } // コピー元デバイスのアクセス許可状態のチェック var rootObjs = GetObjects("DEVICE", content); // ルートオブジェクトが見えるか if (rootObjs.Count() <= 0) { retryMsg = "対象機器へのアクセスが許可されていません\n" + "対象機器でMTP転送を有効後にOKを選択してください"; return(false); } // コピー元フォルダのデバイスIDを取得 existsSrcDir = true; string curDirId = "DEVICE"; foreach (string searchdir in dirpath.TrimStart('\\').TrimEnd('\\').Split('\\')) { //Console.WriteLine(searchdir); bool existCurDir = false; var dirObjs = GetObjects(curDirId, content, TransFileObject.ObjectKind.DIR); foreach (var dirobj in dirObjs) { if (dirobj.fileName == searchdir) { existCurDir = true; curDirId = dirobj.objId; break; } } if (!existCurDir) { existsSrcDir = false; break; } } if (existsSrcDir) { srcDirObjId = curDirId; srcDevId = deviceId; retryMsg = ""; break; } } catch (Exception e) { mainWnd.DispInfo = string.Format("コピー元の検出失敗\n{0}", e.Message); existsSrcDir = false; } finally { device.Close(); } } return(existsSrcDir); }