Search code examples
c#.netopenfiledialogfolderbrowserdialog

c# select Folders or Files in the same method


Okay, so please don't flame me too much, this is my 1st question here, and maybe what I am trying to do is not even possible. Obviously I am not an expert; that's why I am coming to you. :)

I have searched all over here, MSDN, and the rest of the internet (most of which points back here) without any luck. I did see one question asking about using the OpenFileDialog to select a folder instead of a file. I am almost certain that I have seen this in mainstream applications, but the question was marked as being too vague, and that particular caveat was unaddressed in the responses.

I have some text boxes that need file/folder paths. I want to simplify the two methods that handle this, into one. The only difference, is that once selects a file, and the other selects a folder. For simplicity and readability, I'd like to consolidate them.

Is this possible, without literally putting the contents of each code method into a big IF ?

Here are the two methods:

private void FolderBrowser(object sender, EventArgs e)
    {
        TextBox SenderBox = sender as TextBox;
        if (SenderBox.Text != "")//if the text box is not empty
        {
            //set the selected path to the text box's current contents (incase of accidental entry)
            FileBrowserDialog.FileName = SenderBox.Text;
        }
        if (FileBrowserDialog.ShowDialog() == DialogResult.OK)
        {
            SenderBox.Text = FileBrowserDialog.FileName;
        }
    }

private void FileBrowser(object sender, EventArgs e)
    {   //basically the same as the folder browser above, but for selecting specific files
        TextBox SenderBox = sender as TextBox;
        if (SenderBox.Text != "")//if the text box is not empty
        {
            //set the selected path to the text box's current contents (incase of accidental entry)
            FileBrowserDialog.FileName = SenderBox.Text;
        }
        if (FileBrowserDialog.ShowDialog() == DialogResult.OK)
        {
            SenderBox.Text = FileBrowserDialog.FileName;
        }
    }

I have added a Tag to each TextBox, indicating if it needs a file or a folder. I'd like to use the Tag as the condition by which I determine if I should be using a file or folder browser. This is where my ignorance shows; I had envisioned something like this NONWORKING code:

private void browser(object sender, EventArgs e)
    {
        //cast sender as a textbox
        TextBox tBox = (TextBox)sender;
        object browser = null;

        if (tBox.Tag.ToString().Equals("Folder"))
        {
            browser = new FolderBrowserDialog();
        }
        else
        {
            browser = new OpenFileDialog();                
        }

        if (tBox.Text != "")//if the text box is not empty
        {
            //set the selected path to the text box's current contents (incase of accidental entry)
            browser.FileName = tBox.Text;
        }
        if (browser.ShowDialog() == DialogResult.OK)
        {
            tBox.Text = browser.FileName;
        }
    }

Am I crazy, or is there a way to accomplish what I have in mind? To be clear, I want to know if there is:

  1. An existing Object/Method that would allow for the selection of a file or a folder, or
  2. A way to dynamically re-define an object as a different type of object
  3. Any other way to use 1 Method to dynamically allow for the use of OpenFileDialog or FileBrowserDialog based on some Tag defined on the calling object.

Solution

  • This is the easiest way I've found of solving this problem without relying on third party code, but you'll need to add some sanity checks, in case the user goofs around with the input:

            OpenFileDialog ofd = new OpenFileDialog();
            ofd.CheckFileExists = false;
            string defaultFilename = "Select this folder";
            ofd.FileName = defaultFilename;
    
            if (ofd.ShowDialog().Value)
            {
                // Check if the user picked a file or a directory, for example:
                if (!ofd.FileName.Contains(defaultFilename))
                {
                    // File code
                }
                else // You should probably turn this into an else if instead
                {
                    // Directory code
                }
                // Alternatively, but still as unsafe
                if (File.Exists(ofd.FileName))
                {
                    // File code
                }
                else
                {
                    // Directory code
                }
            }
    

    Basically, the "trick" here is to set OpenFileDialog's CheckFileExists to false.