none
Access and copy files from external device (Android phone storage) using C# RRS feed

  • Question

  • Hi All have  a good day,

    I am working in a task where i need to access my External Device storage (Android phone storage) and copy files. I am using FolderBrowserDialog to select the folder, but if i select an external device location the path is not selected or returned.

    Code used:-

    FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
                    if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
                        return folderBrowserDialog.SelectedPath;
                    else
                        return "";

    While using my code, OK button is not enabled.  How can get / select the folder.?

    Thank you.


    Saravanan.M


    Thursday, October 24, 2019 7:38 AM

All replies

  • Hi Saravanan,

    Thank you for posting here.

    For your question, you want to access and copy files from your Android phone storage.

    You could use OpenFileDialog to do that.

    Here is my code:

                OpenFileDialog openFileDialog = new OpenFileDialog();
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        var sr = new StreamReader(openFileDialog.FileName);
                        textBox1.Text = sr.ReadToEnd();
                        //Copy to a different directory.
                        File.Copy(openFileDialog.FileName, @"d:\test\"+Path.GetFileName(openFileDialog.FileName), true);
                    }
                    catch (SecurityException ex)
                    {
                        MessageBox.Show($"Security error.\n\nError message: {ex.Message}\n\n" +
                        $"Details:\n\n{ex.StackTrace}");
                    }
                }

    Hope my solution could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, October 25, 2019 6:35 AM
  • Note that FolderBrowserDialog will only return item that is part of a filesystem.

    To be more specific, it will not be able to return anything that cannot be converted to valid PIDL.

    Galaxy A30 is "device name", which is neither computer name nor valid path, I think this is the expected behaviour.






    Friday, October 25, 2019 6:59 AM
    Answerer
  • Thanks Timon for your reply.

    Your code is working fine for copying a file.

    Actually I need to open a folder, from which i can loop through files and process. If I use FolderBrowserDialog to select an external folder, system not allow to select the external path from a external device location.

    Thanks.


    Saravanan.M


    Friday, October 25, 2019 12:22 PM
  • Actually I need to open a folder, from which i can loop through files and process. If I use FolderBrowserDialog to select an external folder, system not allow to select the external path from a external device location.

    It can be done by P/Invoke with SHBrowseForFolder

    (BIF_BROWSEINCLUDEFILES flag to include files)

    (tested with Archos 40 Helium)
    • Edited by Castorix31 Friday, October 25, 2019 1:51 PM
    Friday, October 25, 2019 1:49 PM