示例#1
0
        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();
        }
示例#2
0
        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();
        }
示例#3
0
        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;
            }
        }
示例#4
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;
            }
        }
示例#5
0
        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 async Task CopyFiles(PortableDevice device, IEnumerable <PortableDeviceObject> files, string path, bool delete, string additionalPath, DateMethod dateMethod, string additionalExtension)
        {
            if (!System.IO.Directory.Exists(path))
            {
                System.IO.Directory.CreateDirectory(path);
            }

            int counter = 0;

            ProgressB.Value   = 0;
            ProgressB.Maximum = files.Count();

            DirectoryInfo di = new DirectoryInfo(path);

            firstF = di.GetDirectories("*.*", System.IO.SearchOption.TopDirectoryOnly).Count();

            string[] existingFiles = (from FileInfo f in di.GetFiles("*.*", SearchOption.AllDirectories)
                                      select f.Name).ToArray();

            PortableDevice pd = collection[DevicesListBox.SelectedIndex];

            foreach (var item in files)
            {
                if (item is PortableDeviceFile)
                {
                    PortableDeviceFile file = (PortableDeviceFile)item;
                    try
                    {
                        if (ignoreCheckBox.IsChecked == true)
                        {
                            if (existingFiles.Contains(file.Name + additionalExtension))
                            {
                                existsCounter++;
                                ProgressMessageLabel.Content = existsCounter.ToString() + " files were already existed. I ignored them.";


                                counter++;
                                ProgressB.Value = counter;
                                continue;
                            }
                        }

                        device.Connect();

                        Exception taskEx = null;
                        await Task.Factory.StartNew(() =>
                        {
                            try
                            {
                                pd.DownloadFile(file, path);
                            }
                            catch (Exception ex)
                            {
                                taskEx = ex;
                            }
                        });

                        if (taskEx != null)
                        {
                            throw taskEx;
                        }

                        device.Disconnect();

                        await CopyFile(path, file.Name, dateMethod, additionalExtension, additionalPath);


                        counter++;
                        ProgressB.Value = counter;

                        if (delete)
                        {
                            device.Connect();
                            await Task.Factory.StartNew(() =>
                            {
                                pd.DeleteFile(file);
                            });

                            device.Disconnect();
                        }
                    }
                    catch (Exception ex)
                    {
                        //MessageBox.Show("Error while copying " + file.Name + "\n\n" + ex.Message);
                    }
                }
            }
        }
示例#7
0
        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;
            }
        }