BNG.exe
BNG.exe

Reputation: 25

How can I use FolderBrowserDialog in Visual Studio 2019?

I'm looking for a way to select a specific directory in C#.

I already tried to use Folder Browser Dialog from the Toolbox but i could not find it there.

DialogResult result = FolderBrowserDialog1.dialog();
if(result==DialogResult.OK)
{
    txtPfad.Text = FolderBrowserDialog1.SelectedPath;
}

Upvotes: 1

Views: 7598

Answers (3)

Vincenzo Lanera
Vincenzo Lanera

Reputation: 162

class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        var folderDialog = new FolderBrowserDialog();
        DialogResult result = folderDialog.ShowDialog();

        if (result == DialogResult.OK)
        {
            string folderPath = folderDialog.SelectedPath;
            //Use folder path
        }
        else
        {
            //Operation aborted by the user
        }
    }
}

You must add also a using to System.Windows.Forms, if you don't see the namespace you must add a reference to the System.Windows.Forms DLL. Follow these steps on Visual Studio 2019:

  1. Open the Solution Explorer Window
  2. Right click on your project
  3. Add/Reference...
  4. Select System.Windows.Forms in the Asseblies/Framework section
  5. Press OK

Upvotes: 3

Roberto Pegoraro
Roberto Pegoraro

Reputation: 1487

Try change de implementation, rather then use dialog, try showDialog like this:

public void ChooseFolder()  
{  
    if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)   
    {  
        textBox1.Text = folderBrowserDialog1.SelectedPath;  
    }  
}  

EDIT: to create a FolderBrowserDialog control at design-time, you simply drag and drop a FolderBrowserDialog control from Toolbox to a Form in Visual Studio. After you drag and drop a FolderBrowserDialog on a Form, the FolderBrowserDialog looks like this Figure

enter image description here

Upvotes: 0

Related Questions