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;
}