【问题标题】:How to transfer the file from Windows Form Application to Xamarin Forms using USB cable C#.netHow to transfer the file from Windows Form Application to Xamarin Forms using USB cable C#.net
【发布时间】:2022-12-27 09:48:37
【问题描述】:

I am now developing Android Application and the Windows Form.

In the previous, the users need to plug in with USB, copy the file and paste into android folder but it would be troublesome and takes time to do.

How can I transfer the file directly from the PC to android Folder (Internal Storage/ downloads) ?

I tried with FolderBrowserDialog to browse and copy into android but "OK" button is disabled when I point the location. enter image description here

Should I write it in Windows Form backend or Xamarin Forms backend side in order to direct transfer into the Android App?

I cannot use Web API as cannot use the internet connection.

FolderBrowserDialog dialog = new FolderBrowserDialog();
                
if (dialog.ShowDialog() == DialogResult.OK)
   {
         txtCopy.Text = dialog.SelectedPath;
   }
         string copyFileName = Path.Combine(txtCopy.Text, originalFile); //originalFile->FileName (testing.jpg)

         File.Copy(txtFile1.Text, copyFileName, true);

Direct file transfer from Windows Form and Xamarin Forms by clicking button

【问题讨论】:

  • CIFS, ftp, scp, custom server, etc. There are many ways you could do this. Which you pick depends on your requirements
  • Perhaps you could work something via Bluetooth. Otherwise, something like Jason has suggested, maybe using something like dropbox or google drive might be the way. Or maybe a webservice that serves these files for the android app to request
  • Thank you for answering. I know how to transfer the data by using Web API but do not have internet access. Only method is transferring file via USB cable. Sadly, I still cannot find how to transfer directly by clicking button instead of drag and drop the files.
  • I for one am still not really 100% sure what the problem is here. Once the device is plugged in and all the PC/Device trusts etc are set, From the PC perspective it's just like any other storage device. Does this work if you skip the FolderBrowserDialog and copy to a hardcoded path?
  • This is an Android emulator running on the PC, with USB plugged into PC? So your problem is how to transfer between host PC and emulator's storage?

标签: c# vb.net xamarin.forms


【解决方案1】:

Based on your code, I made a Windows Form project to complete the copy.

You could refer to the following code:

namespace WinFormsApp1
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void folderBrowserDialog1_HelpRequest(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = dialog.SelectedPath;
            }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.ShowDialog();
            textBox2.Text = dialog.SelectedPath;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DirectoryInfo thefolder = new DirectoryInfo(textBox1.Text);
            foreach (FileInfo nextfile in thefolder.GetFiles()) 
            {
                try
                {
                    string filename = nextfile.Name;
                    string filefullname = nextfile.FullName;
                    string mudi = textBox2.Text + "\" + filename;
                    if (File.Exists(mudi))
                    {
                        //File.Delete(mudi);
                    }
                    else
                    {
                        File.Copy(filefullname, mudi);
                        MessageBox.Show("Successful");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
        }
    }
}

【讨论】:

  • Thank you. But I already used the FolderBrowserDialog but the thing is when I tried to click the folder under the Android Device (eg. "Samsung/Internal Shared Storage/ downloads), I cannot click "OK" in folderDialog to get the folder path." Every folder path can select and click "OK" except the folder under Android device folder.
【解决方案2】:

Now I got it. I have to download MediaDevices framework in NuGet Package Manager to download/ transfer the file from Android device to PC.

I can transfer the file from Android to PC.

But the thing is I cannot transfer/ Upload the file from PC to Android (when using PCToPDA()). When I run the program, the error Director "DT50Internal shared storageDownload" not found appear even though the directory path is correct.

Any idea why is that happening?

    string originalFile;

    private void BtnBrowse_Click(object sender, EventArgs e)
    {
        Browse();     //Get the files from Android Folder
    }

    private void Browse()
    {
        try
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                txtFile1.Text = openFileDialog1.FileName;

                string fileName = Path.GetDirectoryName(openFileDialog1.FileName);
                originalFile = Path.GetFileName(openFileDialog1.FileName);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Browse-ERR", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    private void BtnCopy_Click(object sender, EventArgs e)
    {
        PDAToPC();       //Android to PC
        //PCToPDA();       //PC to Android 
    }

    private void PDAToPC()
    {
        try
        {
            string DeviceNameAsSeenInMyComputer = "DT50";

            var devices = MediaDevice.GetDevices();

            using (var device = devices.Where(d => d.FriendlyName == DeviceNameAsSeenInMyComputer || d.Description == DeviceNameAsSeenInMyComputer).First())
            {
                device.Connect();
                var photoDir = device.GetDirectoryInfo(@"Internal shared storageDownload");
                var files = photoDir.EnumerateFiles("*.*", SearchOption.TopDirectoryOnly);

                
                foreach (var file in files)
                {
                    string destinationFileName = $@"D:KhineTheinTestingTransferTestingphoto{file.Name}";
                    if (!File.Exists(destinationFileName))
                    {
                        using (FileStream fs = new FileStream(destinationFileName, FileMode.Create, System.IO.FileAccess.Write))
                        {
                            device.DownloadFile(file.FullName, fs);
                            
                        }
                    }


                }
                device.Disconnect();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "PDAToPC-ERR", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    private void PCToPDA()
    {
        try
        {
            string DeviceNameAsSeenInMyComputer = "DT50";
            string tempFileName = "CaptureImport.txt";
            var devices = MediaDevice.GetDevices();

            using (var device = devices.Where(d => d.FriendlyName == DeviceNameAsSeenInMyComputer || d.Description == DeviceNameAsSeenInMyComputer).First())
            {
                device.Connect();
                var photoDir = device.GetDirectoryInfo(@"Internal shared storageDownload");
                var files = photoDir.EnumerateFiles("*.*", SearchOption.TopDirectoryOnly);

                foreach (var file in files)
                {
                    string originalFileName = $@"D:KhineTheinTestingTransferTestingphoto{tempFileName}";

                    string transferToFileName = $@"{DeviceNameAsSeenInMyComputer}Internal shared storageDownload{tempFileName}";

                    using (FileStream fs = new FileStream(originalFileName, FileMode.Open, System.IO.FileAccess.Read))
                    {
                        //device.DownloadFile(file.FullName, fs);                       //Path, Stream
                        device.UploadFile(fs, transferToFileName);                      //Stream, Path
                    }


                }
                device.Disconnect();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "PCToPDA-ERR", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

【讨论】:

    猜你喜欢
    • 2022-11-09
    • 2022-12-28
    • 2022-12-27
    • 2022-11-20
    • 2022-12-02
    • 2022-12-01
    • 2022-12-27
    • 2022-12-15
    • 2022-12-27
    相关资源
    最近更新 更多