I want to get a system directory, open a window and in this window I select a directory. I can select files with OpenFileDialog
but no directories.
I want to get a system directory, open a window and in this window I select a directory. I can select files with OpenFileDialog
but no directories.
You can use the FolderBrowserDialog
:
// Instancia a classe.
using (FolderBrowserDialog dirDialog = new FolderBrowserDialog())
{
// Mostra a janela de escolha do directorio
DialogResult res = dirDialog.ShowDialog();
if (res == DialogResult.OK)
{
// Como o utilizador carregou no OK, o directorio escolhido pode ser acedido da seguinte forma:
string directorio = dirDialog.SelectedPath;
}
else
{
// Caso o utilizador tenha cancelado
// ...
}
}
In addition to basic use it can also allow the user to create directories through the window. To do this, place the following property at true
:
dirDialog.ShowNewFolderButton = true;
You can also decide which directory to choose when the window is opened. To do this, use the FolderBrowserDialog.RootFolder
property.