打开文件对话框重用 [英] Open File Dialog reuse

查看:91
本文介绍了打开文件对话框重用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎很简单,但是由于某种原因我无法使其正常工作.

This seems to be very simple but for some reason I cannot get it to work.

我正在构建一个小的C#实用程序,该实用程序使用打开文件对话框"从目录中选择XML文件.那部分工作正常.

I am building a small C# utility that uses Open File Dialog to select an XML file from a directory. That part works fine.

文件被传输到Web服务并收到响应.没问题.

The file is transferred to a web service and a response is received. No problem.

我只是想打开放置响应的DestDirectory.但是,无论我最终以打开文件"对话框结束什么,都是从第一次使用时打开目录.
我尝试了窗体上的打开文件对话框"的两个实例. OpenFD1.Reset(); 以编程方式重置所有值,即initialDirectory

I simply want to open up the DestDirectory where the response is placed. However, no matter what I end up with the Open File Dialog opening up the directory from the first usage.
I have tried two instances of Open File Dialog on the form. OpenFD1.Reset(); Resetting all the value programatically,ie initialDirectory

做我想做的事情似乎很简单,但是我没有找到答案.

It seems very simple to do what I want to do but I am not seeing the answer.

这是代码的一部分:表单上有两个按钮,我想用它们来打开对话框.

Here is part of the code: I have two buttons on the form that I want to use to open up the dialog.

    public void button1_Click(object sender, EventArgs e)
    {          
        OpenFD1.Title = "Open Test File";
        OpenFD1.Filter = "XML Files|*.xml";

        string SourceDir = AppSetting("SourceDir");
        OpenFD1.InitialDirectory = SourceDir;
        if (OpenFD1.ShowDialog() == DialogResult.OK)
        {
            sSelectedFile = OpenFD1.FileName.ToString();
            WFileName.Text = OpenFD1.FileName.ToString();
        }
        OpenFD1.CheckFileExists = true;
        OpenFD1.CheckPathExists = true;
        OpenFD1.InitialDirectory = null; ; 
    }

    private void button3_Click(object sender, EventArgs e)
    {
        OpenFD2.FileName = null;
        OpenFD2.Title = "Test Results";
        OpenFD2.Filter = "XML Files|*.xml";
        string DestDir = AppSetting("DestDir");
        OpenFD2.InitialDirectory = DestDir;
        if (OpenFD2.ShowDialog() == DialogResult.OK)
        {
            sSelectedFile = OpenFD2.FileName.ToString();
            WFileName.Text = OpenFD2.FileName.ToString();
        }
        OpenFD2.CheckFileExists=true;
        OpenFD2.CheckPathExists = true;
    }

推荐答案

在创建OpenFileDialog时尝试设置OpenFD1.RestoreDirectory = false;. RestoreDirectory属性可确保在关闭OpenFileDialog之前重置Environment.CurrentDirectory中的值.

Try to set the OpenFD1.RestoreDirectory = false; when you create your OpenFileDialog. RestoreDirectory property makes sure that the value in Environment.CurrentDirectory will be reset before the OpenFileDialog closes.

这篇关于打开文件对话框重用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆