How do I change the current path for OpenFileDialog ?

  • Thread starter Thread starter Per
  • Start date Start date
P

Per

I want to change the current path in the OpenFileDialog in runtime. When the
user presses OK-button I'm checking the selected path if it's valid for my
application. If it's not valid I want to set the path to a valid one in the
FileOk-event and set the e.Cancel = true.
 
I want to change the current path in the OpenFileDialog in runtime. When the
user presses OK-button I'm checking the selected path if it's valid for my
application. If it's not valid I want to set the path to a valid one in the
FileOk-event and set the e.Cancel = true.

Can't you use Reset() or InitialDirectory() method?
Set a valid initial directory and on invalid path perform a reset() on
the file dialog.
 
CSharper said:
Can't you use Reset() or InitialDirectory() method?
Set a valid initial directory and on invalid path perform a reset() on
the file dialog.

When I do this operation I want the dialog to update and to show the path
that I'm setting in the method that handles the FileOk-event. If I performs a
reset the dialog is not updated to the right path it stays on the wrong path.
 
When I do this operation I want the dialog to update and to show the path
that I'm setting in the method that handles the FileOk-event. If I performs a
reset the dialog is not updated to the right path it stays on the wrong path.

I see your problem. Only way you can do this to make the open dialog
not to validate the the directory exists or not, this can be done by
either one of them. First one for directory validation and second one
is file validation.

openFileDialog1.CheckPathExists = false;
openFileDialog1.CheckFileExists = false;

by default both are set to true;
 
This still did not help. The dialog do not uppdate and show the path that I
set in my event handler for the OK button.
 
This still did not help. The dialog do not uppdate and show the path that I

With the change I suggested, when user press ok button, even if the
file or directory is bad, it should close the fie dialog and you have
to validate the data yourself and if the data is bad show the file
dialog again.
My question, when you press the open button, did the window close or
did it show the error again?

If you have the code, please send it and let me have a look.
Thanks.
 
This is not an option to close the dialog and then open it again.

Here is a code snippet of what I want to do.

private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
string fileName = "";

dlg.FileName = "";
dlg.Title = "Test";
dlg.InitialDirectory = "C:\\Temp";
dlg.CheckFileExists = true;
dlg.CheckPathExists = true;

// Setup event to validate if it's ok to proceed with open
command.
dlg.FileOk += new CancelEventHandler(OnFileOk);

if(dlg.ShowDialog() == DialogResult.OK)
{
fileName = dlg.FileName;
}
}

void OnFileOk(object sender, CancelEventArgs e)
{
OpenFileDialog dlg = sender as OpenFileDialog;

string path = Path.GetDirectoryName(dlg.FileName);

if (path.ToLower() != dlg.InitialDirectory.ToLower())
{
// Not a valid path.

// Here I want to set the dialog to show the
InitalDirectory.
// I want the dialog to show the files under C:\Temp not the
files in
// the right now selected directory that is not valid.


// The Open command is canceled
e.Cancel = true;
}
else
{
// This is a valid path.

// Proceed with the Open command.
e.Cancel = false;
}
}
 
This is not an option to close the dialog and then open it again.

Here is a code snippet of what I want to do.

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            string fileName = "";

            dlg.FileName = "";
            dlg.Title = "Test";
            dlg.InitialDirectory = "C:\\Temp";
            dlg.CheckFileExists = true;
            dlg.CheckPathExists = true;

            // Setup event to validate if it's ok to proceed with open
command.
            dlg.FileOk += new CancelEventHandler(OnFileOk);

            if(dlg.ShowDialog() == DialogResult.OK)
            {
                fileName = dlg.FileName;               
            }
        }

        void OnFileOk(object sender, CancelEventArgs e)
        {
            OpenFileDialog dlg = sender as OpenFileDialog;

            string path = Path.GetDirectoryName(dlg.FileName);

            if (path.ToLower() != dlg.InitialDirectory.ToLower())
            {
                // Not a valid path.

                // Here I want to set the dialog to show the
InitalDirectory.                
                // I want the dialog to show the files under C:\Temp not the
files in
                // the right now selected directory that is not valid.      

                // The Open command is canceled
                e.Cancel = true;               
            }
            else
            {
                // This is a valid path.

                // Proceed with the Open command.
                e.Cancel = false;                
            }

Based on my limited knowledge I think it is not possible with
FileDialog if you do not want to close and open, may be you can create
a custom filedialog inherit from filedialog and in there, overwrite
the open method to handle this situation..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top