Example #1
0
        public static bool getFileFromDevice(PortableDevice device, String deviceFolder, String localPath, String file)
        {
            bool status = false;

            try
            {
                String phoneDir             = deviceFolder;
                PortableDeviceFolder root   = device.Root();
                PortableDeviceFolder result = root.FindDir(phoneDir);
                if (null == result)
                {
                    //Console.Error.WriteLine(phoneDir + " no encontrado!");
                    status = false;
                }
                else
                {
                    PortableDeviceFile deviceFile = ((PortableDeviceFolder)result).FindFile(file);
                    device.TransferContentFromDevice(deviceFile, localPath, file);
                    status = true;
                }
            }
            catch (Exception)
            {
                //Console.Error.WriteLine(ex.Message);
                status = false;
            }
            return(status);
        }
Example #2
0
        public static bool deleteDeviceFile(PortableDevice device, String deviceFolder, String file)
        {
            bool status = false;

            try
            {
                String phoneDir                 = deviceFolder;
                PortableDeviceFolder root       = device.Root();
                PortableDeviceFolder result     = root.FindDir(phoneDir);
                PortableDeviceFile   deviceFile = result.FindFile(file);
                if (null == result)
                {
                    //Console.Error.WriteLine(phoneDir + " no encontrado!");
                    status = false;
                }
                else
                {
                    device.DeleteFile(deviceFile);
                    status = true;
                }
            }
            catch (Exception)
            {
                //Console.Error.WriteLine(ex.Message);
                status = false;
            }
            return(status);
        }
        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 #4
0
        private void btnCopy_Click(object sender, EventArgs e)
        {
            string[] drives = Directory.GetLogicalDrives();
            bool     found  = false;

            foreach (string drive in drives)
            {
                if (File.Exists(drive + "Download\\EpiInfo\\Handshake.xml"))
                {
                    Template template = new Template(mediator);
                    template.CreatePhoneTemplate(drive + "Download\\EpiInfo\\Questionnaires\\" + txtFormName.Text + ".xml");
                    found = true;
                }
            }
            if (!found)
            {
                //try portable device api

                try
                {
                    var devices = new PortableDevices.PortableDeviceCollection();

                    if (devices == null)
                    {
                        Epi.Windows.MsgBox.ShowInformation(SharedStrings.MOBILE_NO_DEVICES);
                        return;
                    }

                    try
                    {
                        devices.Refresh();
                    }
                    catch (System.IndexOutOfRangeException)
                    {
                        Epi.Windows.MsgBox.ShowInformation(SharedStrings.MOBILE_NO_DEVICES);
                        return;
                    }

                    if (devices.Count == 0)
                    {
                        Epi.Windows.MsgBox.ShowInformation(SharedStrings.MOBILE_NO_DEVICES);
                        return;
                    }

                    var pd = devices.First();
                    pd.Connect();
                    PortableDeviceFolder root = pd.GetContents();

                    PortableDeviceFolder download =
                        (from r in ((PortableDeviceFolder)root.Files[0]).Files
                         where r.Name.Equals("Download")
                         select r).First() as PortableDeviceFolder;

                    PortableDeviceFolder epiinfo =
                        (from r in download.Files
                         where r.Name.Equals("EpiInfo")
                         select r).First() as PortableDeviceFolder;

                    PortableDeviceFolder questionnaires =
                        (from r in epiinfo.Files
                         where r.Name.Equals("Questionnaires")
                         select r).First() as PortableDeviceFolder;

                    if (questionnaires == null)
                    {
                        Epi.Windows.MsgBox.ShowInformation(SharedStrings.MOBILE_NO_DEVICES);
                        pd.Disconnect();
                        return;
                    }
                    else
                    {
                        try
                        {
                            PortableDeviceFile existingFile =
                                (from r in questionnaires.Files
                                 where r.Name.Equals(txtFormName.Text + ".xml")
                                 select r).First() as PortableDeviceFile;
                            pd.DeleteFile(existingFile);
                        }
                        catch (Exception ex)
                        {
                            //
                        }

                        Template template = new Template(mediator);
                        template.CreatePhoneTemplate(Path.GetTempPath() + "\\" + txtFormName.Text + ".xml");

                        pd.TransferContentToDevice(Path.GetTempPath() + "\\" + txtFormName.Text + ".xml", questionnaires.Id);
                        found = true;
                    }

                    pd.Disconnect();
                }
                catch (Exception ex)
                {
                }
            }

            if (found)
            {
                MessageBox.Show(SharedStrings.MOBILE_FORM_COPIED, SharedStrings.MOBILE_FORM_COPY_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            else
            {
                MessageBox.Show(SharedStrings.MOBILE_NO_EI_EXT_FOUND, SharedStrings.ERROR, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
 /*
  * Download photo to gallery
  * Directory - date
  * If directory doesn't exits - create it
  * @param PortableDeviceFile - photo to download
  * @param string - gallery path
  * */
 public void downloadPhoto(PortableDeviceFile item, string path)
 {
     string folderPath = System.IO.Path.Combine(path, item.Date);
     if (!System.IO.Directory.Exists(folderPath)) System.IO.Directory.CreateDirectory(folderPath);
     device.DownloadFile(item, System.IO.Path.Combine(folderPath, item.Name));
 }
 /*
  * Get Preview
  * Download file to bufor
  * */
 public void getPreview(PortableDeviceFile item, string deviceName)
 {
     connectToDevice(deviceName);
     try{ device.DownloadFile(item, "data/bufor"); }
     catch (System.IO.IOException ex) { MessageBox.Show(ex.Message); }
     disconnectDevice();
 }