我正在尝试使用 openFileDialog,它一直工作到今天早上我做了一个我认为很简单的改变......

我将过滤器和初始目录属性从硬编码字符串更改为应用程序设置,这就是错误所在。据我所知,一切都应该没问题……我会发布新代码和旧代码……

新代码

    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;
        }

应用程序设置
 Properties.Settings.Default.openFileFilter; = Exe (.exe)|*.exe|MSI (.msi)|*.msi| All (*.*)|*.*
 Properties.Settings.Default.openFileInitialDirectory; = \\\\UNC\\PATH

旧代码
    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;
        }
    }

最佳答案

如果内存正确, Properties.Settings.Default.openFileInitialDirectory 实际上应该只设置为 \\UNC\PATH ,因为字符串已经被转义了。

关于c# - 打开文件对话框...值不在预期范围内,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19051918/

10-16 03:37