문제

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