Вопрос

I am trying to use the openFileDialog, it was working up until this morning when I made what I thought was a simple change....

I changed the Filter and Initial Directory properties from hardcoded strings to application settings and this is where the error comes in. From what I can tell everything should be fine... Ill post the new and old code....

NEW CODE

    private void btnOpenFile_Click(object sender, RoutedEventArgs e)
    {
        OpenFileDialog ofDialog = new OpenFileDialog();
        ofDialog.Filter = Properties.Settings.Default.openFileFilter;
        ofDialog.FilterIndex = 3;
        ofDialog.Multiselect = false;
        ofDialog.InitialDirectory = Properties.Settings.Default.openFileInitialDirectory;

        bool? fileSelected = ofDialog.ShowDialog();

        if(fileSelected == true)
        {
            selectedFileTxtBx.Text = ofDialog.FileName;
        }

Application Settings

 Properties.Settings.Default.openFileFilter; = Exe (.exe)|*.exe|MSI (.msi)|*.msi| All (*.*)|*.*
 Properties.Settings.Default.openFileInitialDirectory; = \\\\UNC\\PATH

Old Code

    private void btnOpenFile_Click(object sender, RoutedEventArgs e)
    {
        OpenFileDialog ofDialog = new OpenFileDialog();
        ofDialog.Filter = "Exe (.exe)|*.exe|MSI (.msi)|*.msi| All (*.*)|*.*";
        ofDialog.FilterIndex = 3;
        ofDialog.Multiselect = false;
        ofDialog.InitialDirectory = "\\\\UNC\\PATH";

        bool? fileSelected = ofDialog.ShowDialog();

        if(fileSelected == true)
        {
            selectedFileTxtBx.Text = ofDialog.FileName;
        }
    }
Это было полезно?

Решение

If memory serves correctly, Properties.Settings.Default.openFileInitialDirectory should actually just be set to \\UNC\PATH, as the string will already be escaped.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top