views:

132

answers:

2

How do I use the OpenFileDialog class (in C#, WPF etc.) such that it opens on the Network area as default?

This does not work:

  OpenFileDialog openFileDialog1 = new OpenFileDialog();
  openFileDialog1.InitialDirectory = "Network";

I also tried having "\" as an InitialDirectory and that did not work.

I also tried having "\\" as an InitialDirectory and that did not work either.

A: 

Customize Your Open File Dialog from the Microsoft MSDN Magazine has a lot of information on the dialog. I haven't had chance to read it all, but this caught my eye:

A Custom Places Bar
...

You'll need a REG_SZ entry if the name of the folder is an absolute or relative path. You need to use the folder-specific number if you want to target a special folder (see Figure 6 for a list). In this case, a REG_DWORD entry is needed.

Figure 6

Folder IDs

ID Folder
0 Desktop
2 Programs folder on Start menu
3 Control Panel
4 Printers
5 My Documents
6 Favorites
7 Startup folder on Start menu
8 Recent Files
9 Send To
10 Recycle Bin
12 Start menu
17 My Computer
18 My Network Places
20 Fonts

I've missed a whole load of stuff out (because it's a very long article), but it looks like you can set the ID value to 18 to get your network places. However, as @Nelson points out this might part looks like it's adding an entry to the bar, so double check it before using. As I said before the post I've linked to contains a lot of information, so what you need may well be buried somewhere in it.

ChrisF
Sounds like that would add it to the bar (as a shortcut), but not actually start there?
Nelson
@Nelson - As I said, I haven't had chance to double check everything on that page, but I thought it would be a useful starting point. I'll update the answer to reflect your doubt.
ChrisF
+3  A: 

I haven't tried it, but this should work:

openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.NetworkShortcuts);

Environment.GetFolderPath returns the path corresponding to an Environment.SpecialFolder enumeration entry as a string.

Environment.SpecialFolder.NetworkShortcuts is defined as

A file system directory that contains the link objects that may exist in the My Network Places virtual folder.

R. Bemrose