Querly Querly - 25 days ago 6
C# Question

How can I use folderBrowserDialog1.SelectedPath in C# to let users select a download path?

I am trying to make a download tool for a project of mine (In C#).
How ever, the users should be able to set a download folder by themselves using

folderBrowserDialog1.SelectedPath
.

Currently I am downloading like this:

private void button1_Click(object sender, EventArgs e)
{
String Pfad = TextBox1.Text;
WebClient Client = new WebClient();
Client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(Client_DownloadProgressChanged);
Client.DownloadFileCompleted += new AsyncCompletedEventHandler(Client_DownloadFileCompleted);
Client.DownloadFileAsync(new Uri("http://dl.4players.de/ts/releases/3.0.13.6/teamspeak3-server_win64-3.0.13.6.zip"), Pfad);
}


As you can see the users have to type in the path, but it should be selectable via
folderBrowserDialog1.SelectedPath
.

thanks!

Answer

I'm not sure what you're stuck on, you've correctly identified that a FolderBrowserDialog would be a good tool to use.

private void button1_Click(object sender, EventArgs e)
{
    // Shows the FolderBrowserDialog and prevents further actions until a choice is made
    DialogResult result = folderBrowserDialog1.ShowDialog(); 

    if (result == DialogResult.OK) // Checks they selected a path
    {
        string Pfad = folderBrowserDialog1.SelectedPath;
        WebClient Client = new WebClient();
        Client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(Client_DownloadProgressChanged);
        Client.DownloadFileCompleted += new AsyncCompletedEventHandler(Client_DownloadFileCompleted);
        Client.DownloadFileAsync(new Uri("http://dl.4players.de/ts/releases/3.0.13.6/teamspeak3-server_win64-3.0.13.6.zip"), Pfad);
    }
}