Question

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?

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top