Take the directory route

Asked

Viewed 3,318 times

3

I want to take a system directory, open a window and in that window I select a directory. I can select files with the OpenFileDialog but directories do not.

2 answers

3


You can use the class 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 the basic use, you can also allow the user to create directories through the window. To do this, set the following property to true:

dirDialog.ShowNewFolderButton = true;

You can also decide which directory to choose when the window is open. To do this use the property FolderBrowserDialog.RootFolder.

1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.