0

I'm trying to let the user choose a folder, here is my code

using (var fbd = new FolderBrowserDialog())
{
    fbd.RootFolder = Environment.SpecialFolder.MyComputer;

    DialogResult result = fbd.ShowDialog();

    if (result == DialogResult.OK)
    {
        //Code
    }
}

However, it appears that DialogResult contains no extension of .OK, I've looked around at some other questions, however none of them seem to work.

Answers tried: DialogResult.OK on SaveFileDialog not work, DialogResult with FolderBrowserDialog in WPF.

I'm probably missing something very obvious...

Sinatr
  • 20,892
  • 15
  • 90
  • 319

2 Answers2

3

You need to add a reference to using System.Windows.Forms;. To do this:

  1. In Solution Explorer, right-click on the References node and choose Add Reference.

  2. Find the System.Windows.Forms and then choose the OK button.

And then first add this to your using directive:

using System.Windows.Forms;

And then:

using (var fbd = new FolderBrowserDialog())
{
    fbd.RootFolder = Environment.SpecialFolder.MyComputer;

    DialogResult result = fbd.ShowDialog();

    if (result == System.Windows.Forms.DialogResult.OK)
    {
        //Code
    }
}
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
-3
MessageBoxResult result = MessageBox.Show("Your Message here", "Your caption", MessageBoxButton.YesNo, MessageBoxImage.Warning <= icon required, MessageBoxResult.No <= default result);

if (result == MessageBoxResult.Yes)
{
    return;
}
stuicidle
  • 297
  • 1
  • 8
  • While this code might answer the question, it is usually considered good practice to add an explanation of what your code does. This allows for developers who are not knowledgeable in this area to understand what is going on in the code and helps them to learn how to solve the problem by them selves in the future. – Caleb Kleveter Dec 22 '17 at 13:41