1

I'm using GetOpenFileName to open files in C++, is it possible to set the initial dir at "Computer" virtual location with lpstrInitialDir?

Thanks, Lee.

Alex K.
  • 171,639
  • 30
  • 264
  • 288
user3725395
  • 145
  • 2
  • 13
  • Have you tried? If not: Why not? – Simon Kraemer Nov 23 '16 at 14:16
  • 1
    In Windows 7 if you have changed current directory in open or save dialog boxes and lpstrInitialDir it is the same as well as the first lpstrInitialDir used by the program when calling first time Open or Save dialogs then the last "current directory" it is used instead of that specified in lpstrInitialDir https://msdn.microsoft.com/it-it/library/windows/desktop/ms646839(v=vs.85).aspx – Ciro Corvino Nov 23 '16 at 14:45
  • I have tried "Computer" that doesn't work as it's not valid. – user3725395 Nov 23 '16 at 14:57
  • I have tried:- SHGetSpecialFolderPath( NULL, lpszPath, CSIDL_DRIVES, false ); with lpszPath and this blank. – user3725395 Nov 23 '16 at 14:57
  • You cannot fetch its path as it has no path `CSILD_DRIVES/FOLDERID_ComputerFolder` is documented as *Default Path: Not applicable—virtual folder* – Alex K. Nov 23 '16 at 15:02
  • so you think it's not possible to start the file dialog up under "Computer" ? – user3725395 Nov 23 '16 at 15:25
  • 2
    @user3725395 - begin from vista you can use `IFileDialog` and set initial folder by `IFileDialog::SetFolder` : `SHGetKnownFolderIDList(FOLDERID_ComputerFolder)`->`SHCreateItemFromIDList`->`SetFolder` – RbMm Nov 23 '16 at 16:31

2 Answers2

3

This is not possible with GetOpenFileName because the location you wish to use is not part of the file system. Rather it is part of the wider shell namespace.

If you look at the documentation for GetOpenFileName you will see that it has been superseded (over 10 years ago in fact) by the Common Item Dialogs. Those dialogs do allow you to specify the initial folder as a shell item.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
1

If you need to support legacy Windows older than Vista, where IFileDialog is not available, try specifying a Shell folder GUID. For example, the My Computer GUID is 20D04FE0-3AEA-1069-A2D8-08002B30309D. You can specify it like this:

ofn.lpstrInitialDir = "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}";

However, it is worth noting that this method is almost doomed to fail on Windows 7 and later, due to behavioral changes

So, you are better off using IFileDialog on Vista and later instead.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
raymai97
  • 806
  • 7
  • 19