Reputation: 5791
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?
Upvotes: 2
Views: 2035
Reputation: 760
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.
Upvotes: 3