如何在WPF中使用OpenFileDialog将目录设置为db中的默认目录?

时间:2014-05-06 16:24:34

标签: c# wpf entity-framework openfiledialog

这是我到目前为止所拥有的:

private void ChangeLotFilePath()
    {
        OpenFileDialog Dialog = new OpenFileDialog();
        Dialog.Filter = "XML files (*.xml)|*.xml";
        Dialog.ShowDialog();

        using (DDataContext context = new DDataContext())
        {
            SystemConfiguration config = context.SysConfig.First();

            if (config != null)
            {
                Dialog.InitialDirectory = config.LotLoadingDirectory;

            }
        }


        if (!String.IsNullOrEmpty(Dialog.FileName))
        {
            LotFileCreationDirectory = Dialog.FileName;
            DeSerializationXML(Dialog.FileName);
        }

    }

出于某种原因,当我设置LotFileCreationDirectory = Dialog.InitialDirectory时,openfiledialog中的目录不会默认为任何内容,或者我收到拒绝访问错误。

我不确定我做错了什么。我只是试图从数据库中获取管理员配置的目录路径,并在openfiledialog打开时将该目录设置为默认值。然后,用户可以从该目录中选择xml文件。

这是我的财产:

private const string LOT_FILE_CREATION_DIRECTORY = "LotFileCreationDirectory";
    public string LotFileCreationDirectory
    {
        get
        {
            return LotFileCreationDirectory;
        }
        set
        {
            LotFileCreationDirectory = value;
            RaisePropertyChanged(LOT_FILE_CREATION_DIRECTORY);
        }
    }

1 个答案:

答案 0 :(得分:1)

您正在显示对话框:

OpenFileDialog Dialog = new OpenFileDialog();
Dialog.Filter = "XML files (*.xml)|*.xml";
Dialog.ShowDialog();

在设置它的InitialDirectory之前

Dialog.InitialDirectory = config.LotLoadingDirectory;