Reputation: 25
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
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:
Upvotes: 3
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
Upvotes: 0
Reputation: 18746
this.fbdSave = new System.Windows.Forms.FolderBrowserDialog();
It lives in the System.Windows.Forms assembly https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/FolderBrowserDialog.cs,3af864744f155c13
Upvotes: 1