0

Everytime i run the program it has an error that says access to the path is denied. i already check the folder allowed all users, unchecked the read only and still it wont work

private void button2_Click(object sender, EventArgs e)
{
    try
    {
        FolderBrowserDialog fbd = new FolderBrowserDialog();
        DialogResult result = fbd.ShowDialog();
        string[] files = Directory.GetFiles(fbd.SelectedPath);
        textBox2.Text = fbd.SelectedPath;
    }
    catch (Exception ex)
    {
          MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

    }
}
Anthon
  • 69,918
  • 32
  • 186
  • 246

2 Answers2

0

Add a check on result.

DialogResult result = fbd.ShowDialog();
if (result == DialogResult.OK)
{
    string[] files = Directory.GetFiles(fbd.SelectedPath);
    textBox2.Text = fbd.SelectedPath;
}
fhnaseer
  • 7,159
  • 16
  • 60
  • 112
0

Check this man..

private void button2_Click(object sender, EventArgs e)
{
    try
    {
        FolderBrowserDialog fbd = new FolderBrowserDialog();
        DialogResult result = fbd.ShowDialog();
        textBox2.Text = (result == DialogResult.OK) ? fbd.SelectedPath : string.Empty;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}
roybalderama
  • 1,650
  • 21
  • 38