Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use the OpenFileDialog class such that it opens on the Network area as default?

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.

like image 866
xarzu Avatar asked May 12 '10 19:05

xarzu


2 Answers

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.

like image 149
Powerlord Avatar answered Oct 25 '22 00:10

Powerlord


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.

Update:

On Windows 7 PC's it doesn't work. eg:

OpenDialogPlaces o = new OpenDialogPlaces();
//o.Places.Add(18);
//o.Places.Add(5);
//o.Places.Add(6);
o.Init();
o.OpenDialog.ShowDialog();
o.Reset();

Still shows everything in the left-hand:

enter image description here

It did work in previous versions of Windows:

enter image description here

Another thing it appears Microsoft has changed the ComDlg32's location, I tried both places but no luck.

enter image description here

like image 36
ChrisF Avatar answered Oct 24 '22 23:10

ChrisF