public virtual void Open(string deviceId) { if (device != null) { return; } // 排他制御用のセマフォを生成する var semName = Regex.Replace("WpdMtpSem" + deviceId, "[^0-9a-zA-Z]", "", RegexOptions.Singleline); if (sem == null) { sem = new Semaphore(1, 1, semName); } else if (!existSemaphoreName.Equals(semName)) { var tempsem = sem; sem = new Semaphore(1, 1, semName); tempsem.Close(); } device = new PortableDevice(); IPortableDeviceValues clientInfo = (IPortableDeviceValues) new PortableDeviceTypesLib.PortableDeviceValues(); device.Open(deviceId, clientInfo); Marshal.ReleaseComObject(clientInfo); // eventを受信できるようにする WpdEvent wpdEvent = new WpdEvent(this); IPortableDeviceValues eventParameter = (IPortableDeviceValues) new PortableDeviceTypesLib.PortableDeviceValues(); device.Advise(0, wpdEvent, eventParameter, out eventCookie); }
public Device(PortableDeviceManager devices, string id) { this.id = id; uint length = 0; devices.GetDeviceDescription(id, null, ref length); char[] bytes = new char[length]; devices.GetDeviceDescription(id, bytes, ref length); description = new string(bytes, 0, (int)length - 1); foobalator.Log.WriteLine(string.Format("Discovered {0}", description)); PortableDeviceValues values = new PortableDeviceValues(); values.SetUnsignedIntegerValue(Constants.WPD_CLIENT_DESIRED_ACCESS, Constants.GENERIC_READ | Constants.GENERIC_WRITE); PortableDevice device = new PortableDevice(); device.Open(id, values); device.Content(out content); content.Properties(out properties); DeviceObject root = new DeviceObject(this, "DEVICE"); DeviceObject phoneFolder = root.GetChildByName("Phone"); if (phoneFolder == null) { return; } musicFolder = phoneFolder.GetChildByName("Music"); if (musicFolder == null) { return; } playlistFolder = musicFolder.GetChildByName("Playlist"); if (playlistFolder == null) { return; } }
/// <summary> /// 连接设备 /// </summary> public bool Connect(string DeviceId) { bool bRet = true; try { IPortableDeviceValues clientInfo = (IPortableDeviceValues) new PortableDeviceTypesLib.PortableDeviceValues(); portableDevice = new PortableDevice(); portableDevice.Open(DeviceId, clientInfo); portableDevice.Content(out deviceContent); deviceContent.Properties(out deviceProperties); deviceProperties.GetValues("DEVICE", null, out deviceValues); AdviceEvent(); } catch (Exception) { bRet = false; } return(bRet); }