private void MenuItem_Click(object sender, RoutedEventArgs e) { DeviceInfo = new XmlDeviceData(); SelectDevice wnd = new SelectDevice(); wnd.ShowDialog(); if (wnd.DialogResult == true && wnd.SelectedDevice != null) { try { SelectedDevice = wnd.SelectedDevice; DeviceDescriptor descriptor = new DeviceDescriptor { WpdId = SelectedDevice.DeviceId }; MtpProtocol device = new MtpProtocol(descriptor.WpdId); device.ConnectToDevice("MTPTester", 1, 0); descriptor.StillImageDevice = device; MTPCamera = new BaseMTPCamera(); MTPCamera.Init(descriptor); LoadDeviceData(MTPCamera.ExecuteReadDataEx(0x1001)); //LoadDeviceData(MTPCamera.ExecuteReadDataEx(0x9108)); PopulateProperties(); } catch (DeviceException exception) { MessageBox.Show("Error getting device information" + exception.Message); } catch (Exception exception) { MessageBox.Show("General error" + exception.Message); } if (DefaultDeviceInfo != null) { foreach (XmlCommandDescriptor command in DeviceInfo.AvaiableCommands) { command.Name = DefaultDeviceInfo.GetCommandName(command.Code); } foreach (XmlEventDescriptor avaiableEvent in DeviceInfo.AvaiableEvents) { avaiableEvent.Name = DefaultDeviceInfo.GetEventName(avaiableEvent.Code); } foreach (XmlPropertyDescriptor property in DeviceInfo.AvaiableProperties) { property.Name = DefaultDeviceInfo.GetPropName(property.Code); } } InitUi(); } }
private void ConnectDevices() { if (_connectionInProgress) { return; } _connectionInProgress = true; if (PortableDeviceCollection.Instance == null) { PortableDeviceCollection.CreateInstance(AppName, AppMajorVersionNumber, AppMinorVersionNumber); PortableDeviceCollection.Instance.AutoConnectToPortableDevice = false; } _deviceEnumerator.RemoveDisconnected(); Log.Debug("Connection device start"); try { var devices = PortableDeviceCollection.Instance.Devices; foreach (PortableDevice portableDevice in devices) { Log.Debug("Connection device " + portableDevice.DeviceId); //TODO: avoid to load some mass storage in my computer need to find a general solution if (!portableDevice.DeviceId.StartsWith("\\\\?\\usb") && !portableDevice.DeviceId.StartsWith("\\\\?\\comp")) { continue; } // ignore some Canon cameras if (!SupportedCanonCamera(portableDevice.DeviceId)) { continue; } portableDevice.ConnectToDevice(AppName, AppMajorVersionNumber, AppMinorVersionNumber); if (_deviceEnumerator.GetByWpdId(portableDevice.DeviceId) == null && GetNativeDriver(portableDevice.Model) != null) { ICameraDevice cameraDevice; DeviceDescriptor descriptor = new DeviceDescriptor { WpdId = portableDevice.DeviceId }; cameraDevice = (ICameraDevice)Activator.CreateInstance(GetNativeDriver(portableDevice.Model)); MtpProtocol device = new MtpProtocol(descriptor.WpdId); device.ConnectToDevice(AppName, AppMajorVersionNumber, AppMinorVersionNumber); descriptor.StillImageDevice = device; cameraDevice.SerialNumber = StaticHelper.GetSerial(portableDevice.DeviceId); cameraDevice.Init(descriptor); if (string.IsNullOrWhiteSpace(cameraDevice.SerialNumber)) { cameraDevice.SerialNumber = StaticHelper.GetSerial(portableDevice.DeviceId); } ConnectedDevices.Add(cameraDevice); NewCameraConnected(cameraDevice); descriptor.CameraDevice = cameraDevice; _deviceEnumerator.Add(descriptor); } } } catch (Exception exception) { Log.Error("Unable to connect to cameras ", exception); } _connectionInProgress = false; }