/// <summary>
        ///     Connect to the portable device
        /// </summary>
        /// <param name="appName"></param>
        /// <param name="majorVersionNumber"></param>
        /// <param name="minorVersionNumber"></param>
        public void ConnectToDevice(string appName, float majorVersionNumber, float minorVersionNumber)
        {
            if (string.IsNullOrEmpty(appName))
            {
                throw new ArgumentNullException("appName");
            }

            //Creating propValues for connection
            var clientValues = (IPortableDeviceValues) new PortableDeviceValuesClass();

            //Set the application name
            _tagpropertykey prop = PortableDevicePKeys.WPD_CLIENT_NAME;

            clientValues.SetStringValue(ref prop, appName);
            //Set the App version
            prop = PortableDevicePKeys.WPD_CLIENT_MAJOR_VERSION;
            clientValues.SetFloatValue(ref prop, majorVersionNumber);
            //Set the app minor version
            prop = PortableDevicePKeys.WPD_CLIENT_MINOR_VERSION;
            clientValues.SetFloatValue(ref prop, minorVersionNumber);

            //Open connection
            PortableDeviceClass.Open(DeviceId, clientValues);

            //Extract device capabilities
            ExtractDeviceCapabilities();

            eventCallback = new PortableDeviceEventCallback(this);
            // According to documentation pParameters should be null (see http://msdn.microsoft.com/en-us/library/dd375684%28v=VS.85%29.aspx )
            PortableDeviceClass.Advise(0, eventCallback, null, out adviseCookie);

            IsConnected = true;
        }
Beispiel #2
0
        public PortableDeviceClass GetDevice(string deviceId)
        {
            PortableDeviceClass deviceClass = new PortableDeviceClass();

            deviceClass.Open(deviceId, _hClientDeviceValues);
            return(deviceClass);
        }
Beispiel #3
0
        public virtual void Connect()
        {
            if (isConnected || string.IsNullOrWhiteSpace(DeviceId))
            {
                return;
            }

            device.Open(DeviceId, clientInfos);

            isConnected = true;
        }
Beispiel #4
0
        public void Connect()
        {
            if (_isConnected)
            {
                return;
            }

            var clientInfo = (IPortableDeviceValues) new PortableDeviceValuesClass();

            PortableDeviceClass.Open(DeviceId, clientInfo);
            _isConnected = true;
        }
        /// <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);
        }
Beispiel #6
0
        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);
        }
Beispiel #7
0
        /// <summary>
        /// create from unique ID, this will attach PodcastUtilities as a client to the specified device
        /// </summary>
        /// <param name="deviceId">ID</param>
        /// <returns>the device</returns>
        public IPortableDevice Create(string deviceId)
        {
            var deviceValues = (IPortableDeviceValues) new PortableDeviceTypesLib.PortableDeviceValuesClass();

            deviceValues.SetStringValue(ref PortableDevicePropertyKeys.WPD_CLIENT_NAME, "PodcastUtilities.PortableDevices");
            deviceValues.SetUnsignedIntegerValue(ref PortableDevicePropertyKeys.WPD_CLIENT_MAJOR_VERSION, 1);
            deviceValues.SetUnsignedIntegerValue(ref PortableDevicePropertyKeys.WPD_CLIENT_MINOR_VERSION, 0);
            deviceValues.SetUnsignedIntegerValue(ref PortableDevicePropertyKeys.WPD_CLIENT_REVISION, 1);

            var device = new PortableDeviceClass();

            device.Open(deviceId, deviceValues);

            return(device);
        }
Beispiel #8
0
        private static IPortableDevice OpenDevice(string deviceId)
        {
            var deviceValues = (IPortableDeviceValues) new PortableDeviceTypesLib.PortableDeviceValuesClass();

            deviceValues.SetStringValue(ref PortableDevicePropertyKeys.WPD_CLIENT_NAME, "Test MTP Client");
            deviceValues.SetUnsignedIntegerValue(ref PortableDevicePropertyKeys.WPD_CLIENT_MAJOR_VERSION, 1);
            deviceValues.SetUnsignedIntegerValue(ref PortableDevicePropertyKeys.WPD_CLIENT_MINOR_VERSION, 0);
            deviceValues.SetUnsignedIntegerValue(ref PortableDevicePropertyKeys.WPD_CLIENT_REVISION, 1);

            var device = new PortableDeviceClass();

            device.Open(deviceId, deviceValues);

            return(device);
        }
Beispiel #9
0
        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>
        /// 连接设备
        /// </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);
        }
        /// <summary>
        /// create from unique ID, this will attach PodcastUtilities as a client to the specified device
        /// </summary>
        /// <param name="deviceId">ID</param>
        /// <returns>the device</returns>
        public IPortableDevice Create(string deviceId)
        {
            var deviceValues = (IPortableDeviceValues)new PortableDeviceTypesLib.PortableDeviceValuesClass();

            deviceValues.SetStringValue(ref PortableDevicePropertyKeys.WPD_CLIENT_NAME, "PodcastUtilities.PortableDevices");
            deviceValues.SetUnsignedIntegerValue(ref PortableDevicePropertyKeys.WPD_CLIENT_MAJOR_VERSION, 1);
            deviceValues.SetUnsignedIntegerValue(ref PortableDevicePropertyKeys.WPD_CLIENT_MINOR_VERSION, 0);
            deviceValues.SetUnsignedIntegerValue(ref PortableDevicePropertyKeys.WPD_CLIENT_REVISION, 1);

            var device = new PortableDeviceClass();
            try
            {
                device.Open(deviceId, deviceValues);
            }
            catch (System.Exception)
            {
                //Device has been detached meanwhile
                device = new PortableDeviceClass();
            }
            return device;
        }
Beispiel #12
0
        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);
        }
Beispiel #13
0
        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());
        }
Beispiel #14
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);
        }
Beispiel #15
0
        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);
        }