简体   繁体   English

文件打开/保存对话框

[英]File Open/Save Dialog

I am using my own Custom View to show the files and folders and also using a search box to jump to a specific folder. 我使用自己的“自定义视图”显示文件和文件夹,还使用搜索框跳转到特定文件夹。 In that case How to send a message to File Open/Save dialog to enforce it to change the current displayed folder. 在这种情况下,如何将消息发送到“文件打开/保存”对话框以强制其更改当前显示的文件夹。

eg If the dialog shows files and folders of current displaying folder "C:\\", I want an API (or any piece of code) to enforce to change the current folder to "D:\\" 例如,如果对话框显示当前显示文件夹“ C:\\”的文件和文件夹,则我要强制执行A​​PI(或任何代码段)以将当前文件夹更改为“ D:\\”

You can have the dialog open at a specific directory using InitialDirectory . 您可以使用InitialDirectory在特定目录中打开对话框。

If you want to control what the dialog does at runtime, that's a bit more complex. 如果要控制对话框在运行时执行的操作,则要复杂一些。

Set SaveFileDialog.InitialDirectory after you create it, but before you open it. 在创建SaveFileDialog.InitialDirectory之后,但在将其打开之前,请对其进行设置。

For example: 例如:

Stream myStream = null;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();

saveFileDialog1 .InitialDirectory = "d:\\" ;
saveFileDialog1 .Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
saveFileDialog1 .FilterIndex = 2 ;
saveFileDialog1 .RestoreDirectory = true ;

if(saveFileDialog1 .ShowDialog() == DialogResult.OK)
{
    try
    {
        if ((myStream = saveFileDialog1 .OpenFile()) != null)
        {
            // Code to write the stream goes here.
            myStream.Close();

        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error: Could not save file to disk. Original error: " + ex.Message);
    }
}

InitialDirectory属性设置为任何路径

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM