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?

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top