I am writing some software in C# which will detect when an external (not part of my application) OpenFileDialog/SaveFileDialog window is open (in any application) and I need to both get the current directory of the OpenFileDialog/SaveFileDialog and re-direct it to another location on a certain event.
For example, a user opens Microsoft Word, selects File -> Open and the OpenFileDialog opens to the directory C:\Users\Username\Documents. I am in need of a way of obtaining the "C:\Users\Username\Documents\" string value and also a way of re-directing this OpenFileDialog to a new directory. For example: C:\Users.
I understand I can use the following code to get (and re-direct the directory) of open Windows Explorer windows:
var ShellWindows = new SHDocVw.ShellWindows();
foreach (SHDocVw.InternetExplorer ShellWindow in ShellWindows)
{
string ShellWindowFullNameUpperCase = ShellWindow.FullName.ToUpper();
if (ShellWindowFullNameUpperCase.Equals("C:\\WINDOWS\\EXPLORER.EXE"))
{
ShellWindow.Navigate(SelectedFilePath);
break;
} // end if.
} // end for/each.
I have read on other posts on Stack Overflow that the OpenFileDialog/SaveFileDialog cannot be accessed in the same way.
I have written two methods to detect if the focused window is an OpenFileDialog or a SaveFileDialog:
if (IsOpenFileDialog(ForegroundWindow))
{
Debug.WriteLine("OpenFileDialog Detected");
}
else if (IsSaveFileDialog(ForegroundWindow))
{
Debug.WriteLine("SaveFileDialog Detected");
} // end if.
I have the handle for these windows although I cannot seem to figure out how to get the current directory/set the directory to a new value (as explained above).
Any assistance anyone can give would be much appreciated.
Thanks in advance.