简体   繁体   中英

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:\\"

You can have the dialog open at a specific directory using 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.

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属性设置为任何路径

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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