以编程方式更改“打开文件”对话框中的目录

发布于 2024-08-31 02:29:50 字数 205 浏览 6 评论 0原文

我有一个带有组合框的自定义 OpenFileDialog(VS2008、C#、Windows 窗体)。组合框将有一个用户可以选择的路径列表。

我的问题是,有没有办法可以更改“打开文件”对话框中的目录以指向组合框所选项目中的路径。

InitialDirectory 仅在打开对话框之前起作用,我想要一种在对话框打开后以编程方式更改目录的方法。

谢谢

I have a customized OpenFileDialog (VS2008, C#, Windows Forms) with a ComboBox. The ComboBox will have a list of paths which the user can select.

My question, is there a way I can change the directory in Open File Dialog to point to the path in the combobox selected item.

InitialDirectory works only before I open the dialog, I wanted a way to change the directory programatically after the dialog is open.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

霞映澄塘 2024-09-07 02:29:50

如果您使用的是带有 .NET 3.5 SP1 的 Vista 或 Windows 7,我建议您使用 OpenFileDialog 上的 CustomPlaces 属性,而不是自定义组合框。

请参阅此 MSDN 文章(适用于 WPF): http://msdn.microsoft.com/en-us/library/microsoft.win32.filedialog.customplaces(v=VS.100).aspx

或此 MSDN 文章(适用于 Windows 窗体): http:// /msdn.microsoft.com/en-us/library/system.windows.forms.filedialog.customplaces(v=VS.100).aspx

在 Windows 2000 和 XP 上,还可以自定义地点侧边栏。但它更困难,并且需要您使用一些 C++ 代码(通过 CLI/C++ 可能是最好的)。此 MSDN 文章详细介绍了该技术: http://msdn.microsoft .com/en-us/magazine/cc300434.aspx

如果您执意要使用已添加到 OpenFileDialog 的组合框,那么您可能只需要知道要发送到对话框的 Windows 消息。恐怕我不知道您需要发送哪条消息。常见打开/保存对话框的令人讨厌的内部 Win32 API 详细信息如下: http://msdn.microsoft.com/en-us/library/ms646960(VS.85).aspx

如果您能找出将哪些消息发送到窗口的可能的处理方式的方法是用要切换的目录填充文件名文本字段以模拟“确定”按钮单击。如果执行此操作,对话框将切换到该目录。

向此窗口发送消息可能需要您不直接使用 OpenFileDialog,而是对其所基于的抽象 FileDialog 类进行子类化。

If you're using Vista or Windows 7 with .NET 3.5 SP1 I recommend you use the CustomPlaces property on OpenFileDialog rather than a custom combo box.

See this MSDN article (for WPF): http://msdn.microsoft.com/en-us/library/microsoft.win32.filedialog.customplaces(v=VS.100).aspx

Or this MSDN article (for Windows Forms): http://msdn.microsoft.com/en-us/library/system.windows.forms.filedialog.customplaces(v=VS.100).aspx

On Windows 2000 and XP it is also possible to customize the places side bar. But it is more difficult and requires you to use some C++ code (via CLI/C++ is probably best). The technique is described in detail in this MSDN article: http://msdn.microsoft.com/en-us/magazine/cc300434.aspx

If you're dead set on using a combo box you've added to the OpenFileDialog then you will probably just need to know what windows message to send to the dialog. I'm afraid I don't know which message you need to send. The nasty internal Win32 API details of the Common Open/Save dialog is detailed here: http://msdn.microsoft.com/en-us/library/ms646960(VS.85).aspx

If you can figure out which messages to send to the window the probably way of doing things is to fill the filename text field with the directory you want to switch to simulate a OK button click. The dialog will switch to that directory if you do this.

Sending messages to this window will probably require you to not use OpenFileDialog directly but rather subclass the abstract FileDialog class upon which it is based.

野稚 2024-09-07 02:29:50

只需设置 openFileDialog1 的 InitialDirectory 属性

private void button1_Click(object sender, System.EventArgs e)
{
    Stream myStream = null;
    OpenFileDialog openFileDialog1 = new OpenFileDialog();

    openFileDialog1.InitialDirectory = cmbPath.SelectedValue.ToString();
    openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
    openFileDialog1.FilterIndex = 2 ;
    openFileDialog1.RestoreDirectory = true ;

    if(openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        try
        {
            if ((myStream = openFileDialog1.OpenFile()) != null)
            {
                using (myStream)
                {
                    // Insert code to read the stream here.
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
        }
    }
}

Just set the InitialDirectory property of openFileDialog1

private void button1_Click(object sender, System.EventArgs e)
{
    Stream myStream = null;
    OpenFileDialog openFileDialog1 = new OpenFileDialog();

    openFileDialog1.InitialDirectory = cmbPath.SelectedValue.ToString();
    openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
    openFileDialog1.FilterIndex = 2 ;
    openFileDialog1.RestoreDirectory = true ;

    if(openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        try
        {
            if ((myStream = openFileDialog1.OpenFile()) != null)
            {
                using (myStream)
                {
                    // Insert code to read the stream here.
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
        }
    }
}
友欢 2024-09-07 02:29:50

正如已经说过的,InitialDirectory 事先就可以工作,但是为什么要在事后更改文件夹呢? FileOpenDialog 是一个模式对话框,因此用户不能使用该对话框之外的应用程序的任何其他内容。不想设置文件夹的好处和原因是什么?您似乎使用了错误的工具来完成工作。

As already said InitialDirectory works before hand but why would you change the folder afterwords? FileOpenDialog is a modal dialog, therefore the user can't use anything else of your application than the dialog. What is the benefit and reason why you wan't to set the folder? It seems your using the wrong tools to get the job done.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文