2

So currently I get user decided path to file this way:

private void Button_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();

            // Set filter for file extension and default file extension 
            dlg.DefaultExt = ".xml";
            dlg.Filter = "XML files (*.xml)|*.xml";

            // Display OpenFileDialog by calling ShowDialog method 
            Nullable<bool> result = dlg.ShowDialog();

            if (result == true)
            {
                // Open document 
                string xmlFile = dlg.FileName; // this required full path.

            }

        }

It works fine, but usually default path is .exe locating folder and need to change it. How can I do it?

1
  • Are you wanting to change the directory the dialog opens to when you do .ShowDialog()? Commented Nov 26, 2013 at 20:16

1 Answer 1

3

If you are wanting to have the dialog open to a specific directory when .ShowDialog() is called you can set the InitialDirectory property to whatever path pleases you.

When you do this it's good practice to set the OpenFileDialog to null when you are finished.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes it works this way dlg.InitialDirectory = _directoryName1;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.