Example #1
0
        public static void getFileFromDevice(String deviceFolder, String localPath, String file)
        {
            PortableDevice currentDevice = FileManagerModel.getCurrentDevice();

            try
            {
                if (FileManagerModel.searchDeviceFile(currentDevice, deviceFolder, file))
                {
                    FileManagerModel.getFileFromDevice(currentDevice, deviceFolder, localPath, file);
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.WriteLine("El archivo " + file + " fue movido a la PC");
                    Console.ResetColor();
                    currentDevice.Disconnect();
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Error.WriteLine("No se encontro el archivo en el dispositivo");
                    Console.ResetColor();
                    currentDevice.Disconnect();
                }
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Error.WriteLine(ex.Message);
                Console.ResetColor();
                currentDevice.Disconnect();
            }
        }
Example #2
0
        public static void sendFileToDevice(String deviceFolder, String localPath, String localFile, String newNameFile)
        {
            PortableDevice currentDevice = FileManagerModel.getCurrentDevice();

            try
            {
                if (FileManagerModel.searchDeviceFile(currentDevice, deviceFolder, localFile))
                {
                    FileManagerModel.deleteDeviceFile(currentDevice, deviceFolder, localFile);
                }
                FileManagerModel.sendFileToDevice(currentDevice, deviceFolder, localPath, localFile, newNameFile);
                Console.ForegroundColor = ConsoleColor.White;
                if (FileManagerModel.searchDeviceFile(currentDevice, deviceFolder, localFile))
                {
                    Console.WriteLine("El archivo " + localFile + " fue movido al dispositivo");
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Error.WriteLine("No se pudo enviar el archivo " + localFile + " al dispositivo");
                }
                Console.ResetColor();
                currentDevice.Disconnect();
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Error.WriteLine(ex.Message);
                Console.ResetColor();
                currentDevice.Disconnect();
            }
        }
Example #3
0
        public static bool searchDeviceFile(String deviceFolder, String file)
        {
            bool           status        = false;
            PortableDevice currentDevice = FileManagerModel.getCurrentDevice();

            try
            {
                if (FileManagerModel.searchDeviceFile(currentDevice, deviceFolder, file))
                {
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.WriteLine("El archivo " + file + " fue encontrado");
                    Console.ResetColor();
                    status = true;
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Error.WriteLine(file + " no encontrado!");
                    Console.ResetColor();
                    status = false;
                }
                currentDevice.Disconnect();
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Error.WriteLine(ex.Message);
                Console.ResetColor();
                status = false;
                currentDevice.Disconnect();
            }
            return(status);
        }
Example #4
0
        public static void deleteDeviceFile(String deviceFolder, String file)
        {
            PortableDevice currentDevice = FileManagerModel.getCurrentDevice();

            try
            {
                if (FileManagerModel.searchDeviceFile(currentDevice, deviceFolder, file))
                {
                    FileManagerModel.deleteDeviceFile(currentDevice, deviceFolder, file);
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.Error.WriteLine("El archivo " + file + " fue eliminado");
                    Console.ResetColor();
                    currentDevice.Disconnect();
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Error.WriteLine("Error al eliminar, no se encontro el archivo");
                    Console.ResetColor();
                    currentDevice.Disconnect();
                }
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Error.WriteLine(ex.Message);
                Console.ResetColor();
                currentDevice.Disconnect();
            }
        }
Example #5
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();
        }
Example #6
0
        public static int DeviceInfoCommand(CommandLineItemCollection commandLine)
        {
            int?exitCode;

            PrintCommandName();

            var deviceId      = commandLine.GetArgument(0)?.Value;
            var directoryPath = commandLine.GetArgument(1)?.Value;

            PortableDevice device = default;

            try
            {
                PortableDeviceFolder directory;
                (exitCode, device, directory) = ConnectAndGetDeviceAndDirectory(deviceId, directoryPath);

                if (exitCode.HasValue)
                {
                    return(exitCode.Value);
                }

                PrintDirectory(directory);
            }
            finally
            {
                device?.Disconnect();
            }

            Console.WriteLine($"[{nameof(EXIT_CODE_OK)}]");
            return(EXIT_CODE_OK);
        }
Example #7
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();
        }
Example #8
0
        public static bool checkDevice()
        {
            bool           status        = false;
            PortableDevice currentDevice = FileManagerModel.getCurrentDevice();

            try
            {
                if (currentDevice == null)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Error.WriteLine("Dispositivo no encontrado!");
                    status = false;
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.WriteLine("Dispositivo encontrado: " + currentDevice.FriendlyName);
                    Console.WriteLine("Dispositivo con Android");
                    Console.ResetColor();
                    status = true;
                    currentDevice.Disconnect();
                }
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Error.WriteLine(ex.Message);
                Console.ResetColor();
                status = false;
                if (currentDevice != null)
                {
                    currentDevice.Disconnect();
                }
            }
            return(status);
        }
Example #9
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;
            }
        }
Example #10
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;
            }
        }
Example #11
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;
                }
            });
        }
Example #12
0
        public static int TransferCommand(CommandLineItemCollection commandLine)
        {
            int?exitCode;

            PrintCommandName();

            var deviceId            = commandLine.GetArgument(0)?.Value;
            var sourceDirectoryPath = commandLine.GetArgument(1)?.Value;
            var targetDirectoryPath = commandLine.GetArgument(2)?.Value;

            if (string.IsNullOrEmpty(targetDirectoryPath) || !Directory.Exists(targetDirectoryPath))
            {
                Console.WriteLine($"[{nameof(EXIT_CODE_TARGET_DIRECTORY_NOT_FOUND)}]");
                return(EXIT_CODE_TARGET_DIRECTORY_NOT_FOUND);
            }

            var tempTargetDirectoryPath = Path.Combine(targetDirectoryPath, $".{nameof(BlinctureMTP)}-temp", Guid.NewGuid().ToString("N"));

            SetHidden(CreateDirectoryIfMissing(tempTargetDirectoryPath));

            PortableDevice       device = default;
            PortableDeviceFolder directory;

            try
            {
                (exitCode, device, directory) = ConnectAndGetDeviceAndDirectory(deviceId, sourceDirectoryPath);
                if (exitCode.HasValue)
                {
                    return(exitCode.Value);
                }

                foreach (var file in directory.Files.OfType <PortableDeviceFile>())
                {
                    Spinner.Start($"Transfering '{file.Name}' ...", spinner =>
                    {
                        var flagTargetFilePath = Path.Combine(targetDirectoryPath, $".{nameof(BlinctureMTP)}-flags", file.Name);
                        SetHidden(CreateContainingDirectoryIfMissing(flagTargetFilePath));
                        if (File.Exists(flagTargetFilePath))
                        {
                            spinner.Info("File already transferred.");
                            return;
                        }

                        var tempTargetFilePath = Path.Combine(tempTargetDirectoryPath, file.Name);
                        string targetFileName;
                        using (var tempTargetFileStream = new FileStream(tempTargetFilePath, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.Read))
                        {
                            var data = device.DownloadFileToStream(file);
                            tempTargetFileStream.Write(data, 0, data.Length);

                            tempTargetFileStream.Position = 0;
                            targetFileName = GetTargetFileName(commandLine, file.Name, tempTargetFileStream, spinner);
                            tempTargetFileStream.Close();
                        }

                        var targetFilePath = Path.Combine(targetDirectoryPath, targetFileName);
                        CreateContainingDirectoryIfMissing(targetFilePath);
                        File.Move(tempTargetFilePath, targetFilePath);

                        File.WriteAllText(flagTargetFilePath, DateTimeOffset.Now.ToString("O"), Utf8Encoding);
                        spinner.Succeed($"Transferred '{file.Name}' as '{targetFileName}'.");
                    });
                }
            }
            finally
            {
                device?.Disconnect();
            }

            Directory.Delete(tempTargetDirectoryPath);

            Console.WriteLine($"[{nameof(EXIT_CODE_OK)}]");
            return(EXIT_CODE_OK);
        }
        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);
                    }
                }
            }
        }
Example #14
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;
            }
        }