views:

460

answers:

3

Hi All,

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

A: 

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);
        }
    }
}
Oscar Cabrero
the combo box is embedded in the same customized form as the open dialog. Hence setting the initial directory wont work as the dialog is already open.
ofarooq
A: 

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.

PoweRoy
The Combo Box will have the most recent used paths, which the user can use. It is actually emulating the FileOpen in 3Ds Max.
ofarooq
can you post a video? You want a sort of history list of recent used files? Create your own form that combines the use of the FileOpenDialog and your own needs?
PoweRoy
+1  A: 

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.

orj