static void Main() { //Connect to MTP devices and pick up the first one var devices = new PortableDeviceCollection(); devices.Refresh(); Tablet = devices.First(); Tablet.Connect(); //Getting root directory var root = Tablet.GetContents(); //Displayinh directory content in console foreach (var resource in root.Files) { DisplayResourceContents(resource); } //Finding neccessary folder inside the root var folder = (root.Files.FirstOrDefault() as PortableDeviceFolder). Files.FirstOrDefault(x => x.Name == "Folder") as PortableDeviceFolder; //Finding file inside the folder var file = (folder as PortableDeviceFolder)?.Files?.FirstOrDefault(x => x.Name == "File"); //Transfering file into byte array var fileIntoByteArr = Tablet.DownloadFileToStream(file as PortableDeviceFile); //Transfering file into file system Tablet.DownloadFile(file as PortableDeviceFile, "\\LOCALPATH"); //Transfering file rom file system into device folder Tablet.TransferContentToDevice("\\LOCALPATH", folder.Id); //Transfering file from stream into device folder var imgPath = "\\LOCALPATH"; var image = Image.FromFile(imgPath); byte[] imageB; using (var ms = new MemoryStream()) { image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); imageB = ms.ToArray(); } Tablet.TransferContentToDeviceFromStream("FILE NAME", new MemoryStream(imageB), folder.Id); //Close the connection Tablet.Disconnect(); Console.WriteLine(); Console.WriteLine("Press any key to continue."); Console.ReadKey(); }
private void connectWithDevice(object state) { if (detected) { return; } detected = true; Console.WriteLine("Device detected"); var devices = new PortableDeviceCollection(); devices.Refresh(); device = devices.First(); device.Connect(); Console.WriteLine("Connected to: " + device.FriendlyName); var root = device.GetContents(); servicePlatformFolder = (PortableDeviceFolder)device.getServicePlatformFolder(); if (servicePlatformFolder == null) { Console.WriteLine("Could not find ServicePlatform folder, have you installed ServicePlatform mobile app? Disconnecting..."); device.Disconnect(); return; } getServicesList(device, servicePlatformFolder); if (!servicesFileDetected) { Console.WriteLine("Could not detect services! Disconnecting..."); device.Disconnect(); return; } BeginInvoke(new MethodInvoker(delegate { Show(); //MessageBox.Show("Connected to: " + device.FriendlyName); })); cleanup(device, servicePlatformFolder); //device.Disconnect(); }
private void ShowFileCountNotification(Device connectedDevice) { PortableDevice portableDevice = null; try { if (_connectedDeviceCollection != null && _connectedDeviceCollection.Count > 0) { //get the connect device of same Id portableDevice = _connectedDeviceCollection. SingleOrDefault(p => p.DeviceId.Equals(connectedDevice.Id)); if (portableDevice != null) { portableDevice.Connect(); //set active device to curren-device _activeDevice = connectedDevice; PortableDeviceFolder deviceFolder = portableDevice.GetContents(); var fileCount = EnumerateContents(portableDevice, deviceFolder); Console.WriteLine("Total number of files found {0}", fileCount); if (_fileCounter > 0) { SystemTrayManager.ShowBalloonTip(balloonTipText: string.Format("{0} files found in - {1} " , _fileCounter, connectedDevice.DisplayName)); } } } } catch (Exception) { throw; } finally { if (portableDevice != null) { portableDevice.Disconnect(); } _activeDevice = null; _fileCounter = 0; } }
private void CopyDeviceContents(Device device) { //if (System.Threading.Thread.CurrentThread.IsBackground) // Utility.LogMessage("[CopyDeviceContents] [Task] [IsBackground][Device::{0}]",device.DisplayName); PortableDevice portableDevice = null; try { if (_connectedDeviceCollection != null && _connectedDeviceCollection.Count > 0) { //get the connect device of same Id portableDevice = _connectedDeviceCollection. SingleOrDefault(p => p.DeviceId.Equals(device.Id)); if (portableDevice != null) { portableDevice.Connect(); //set active device to current-device Utility.LogMessage(@"[CopyDeviceContents] [Setting_ActiveDevice] [{0}]", device.DisplayName); _activeDevice = device; PortableDeviceFolder deviceFolder = portableDevice.GetContents(); FetchContents(portableDevice, deviceFolder); } } } catch (Exception ex) { Utility.LogException(ex); } finally { if (portableDevice != null) { portableDevice.Disconnect(); } Utility.LogMessage(@"[CopyDeviceContents] [Finally] [Setting_ActiveDevice][NULL]"); _activeDevice = null; } }
private async Task CopyDeviceContents(Device device) { Task.Run(() => { PortableDevice portableDevice = null; try { if (_connectedDeviceCollection != null && _connectedDeviceCollection.Count > 0) { //get the connect device of same Id portableDevice = _connectedDeviceCollection. SingleOrDefault(p => p.DeviceId.Equals(device.Id)); if (portableDevice != null) { portableDevice.Connect(); //set active device to curren-device _activeDevice = device; PortableDeviceFolder deviceFolder = portableDevice.GetContents(); FetchContents(portableDevice, deviceFolder); } } } catch (Exception ex) { //log exception } finally { if (portableDevice != null) { portableDevice.Disconnect(); } _activeDevice = null; } }); }
private void ShowFileCountNotification(Device connectedDevice) { if (System.Threading.Thread.CurrentThread.IsBackground) { Utility.LogMessage("[ShowFileCountNotification] [IsBackground]"); } else { Utility.LogMessage("[ShowFileCountNotification] [Foreground]"); } PortableDevice portableDevice = null; try { if (_connectedDeviceCollection != null && _connectedDeviceCollection.Count > 0) { //get the connect device of same Id portableDevice = _connectedDeviceCollection. SingleOrDefault(p => p.DeviceId.Equals(connectedDevice.Id)); if (portableDevice != null) { portableDevice.Connect(); //set active device to curren-device Utility.LogMessage(@"[ShowFileCountNotification] [Setting_ActiveDevice] [{0}]", connectedDevice.DisplayName); _activeDevice = connectedDevice; PortableDeviceFolder deviceFolder = portableDevice.GetContents(); var fileCount = EnumerateContents(portableDevice, deviceFolder); Utility.LogMessage("[ShowFileCountNotification] [Total number of files found -> {0}][Device::{1}]", fileCount, connectedDevice.DisplayName); if (_validFileCounter > 0) { SystemTrayManager.ShowBalloonTip(balloonTipText: string.Format("{0} files to be copied, found in - {1} " , _validFileCounter, connectedDevice.DisplayName)); Utility.LogMessage(string.Format("{0} files to be copied, found in - {1} " , _validFileCounter, connectedDevice.DisplayName)); } } } } catch (Exception) { throw; } finally { if (portableDevice != null) { portableDevice.Disconnect(); } Utility.LogMessage(@"[ShowFileCountNotification] [Finally] [Setting_ActiveDevice][NULL]"); _activeDevice = null; _validFileCounter = 0; _totalFileCounter = 0; } }