0

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.

Jonathan
  • 147
  • 1
  • 2
  • 10
  • Just one question: **why?** – Patrick Hofman Jun 17 '19 at 10:09
  • @PatrickHofman I am building an application to enhance the "Suggested Folders" function in Windows with new data. A list of relevant directories will be displayed and when clicked, I want to navigate the OpenFileDialog/SaveFileDialog to the directory. – Jonathan Jun 17 '19 at 10:12
  • So why not register yourself a pinned folder? https://stackoverflow.com/q/36739317/993547 You can pick it up from there. – Patrick Hofman Jun 17 '19 at 10:12
  • The net method is a wrapper that calls the Operating System method so the two should be the same. There are many options to the Windows Dialog method and Net does not allow all the options so sometimes you have to call Windows Version. the Shell Window version is designed to work with Linux Operating System so it may behave different from Windows. In Windows I usually set the .InitialDirectory property before Show Dialog. – jdweng Jun 17 '19 at 10:19
  • @jdweng I think OP wants to change the dialog for Word, Excel, etc for example. Not their own. Setting `InitialDirectory` is not possible then. – Patrick Hofman Jun 17 '19 at 10:25
  • Excel VBA you can call the Windows Dialog method instead of the default VBA version. I've done it lots of times. – jdweng Jun 17 '19 at 10:29
  • 1
    If the path is "yours", then you can write a custom "shell namespace extension". So for example, if you want to show a "Suggested Folders" virtual folder (with a list of virtual items in it that would redirect to real folders), then, you can do it. Otherwise, only hacks (hooks) can achieve that with uncertain (and certainly not supported) outcomes. – Simon Mourier Jun 17 '19 at 10:53
  • Thank you for all your comments/suggestions. @PatrickHofman I am checking if this is a viable option but it's a great suggestion thanks. – Jonathan Jun 17 '19 at 11:34
  • @SimonMourier Thanks. I have a list of file paths and when one is clicked, I wish to re-direct the active OpenFileDialog (regardless of application) to it. Does this fall in line with what a shell namespace extension could handle? – Jonathan Jun 17 '19 at 11:34
  • If these paths would be displayed in a specific "virtual" folder, like "quick access", "this PC", or "network"... yes – Simon Mourier Jun 17 '19 at 11:36
  • @SimonMourier Ah I see. So if I had added them to the "Quick Access" list this can be accomplished but if they are in a standard listbox in an external application for example, this isn't possible without hacking around with hooks? – Jonathan Jun 17 '19 at 11:40
  • "Quick Access" is just an example and has its own rules, but you can add any folder like this one. – Simon Mourier Jun 17 '19 at 12:52

0 Answers0