2

I am using VS2013 Express for Desktop and using Windows Forms.

I am following this MSDN Walkthrough "Creating an Explorer Style Interface with the ListView and TreeView Controls Using the Designer". So I am trying to get the treeview to start from the "MyComputer" level, so that all the drive letters are available for the user to pick.

It works for MyDocuments, as follows:

DirectoryInfo info = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));

But it doesn't work when I pick MyComputer from the autocomplete:

DirectoryInfo info = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyComputer));

I get:

An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll Additional information: The path is not of a legal form.

This is surprising because "MyComputer" is available in the autocomplete, but doesn't work like the other specialfolders.

Thank you.

UPDATE: Thank you ByteBlast and CodyGray.

Would it be appropriate to ask here what I should do instead?

Doochz
  • 1,039
  • 2
  • 14
  • 25
  • 1
    This would be a good time to learn how to use a debugger. Create an extra variable and put the result of Environment.GetFolderPath(Environment.SpecialFolder.MyComputer) into it. Then use the debugger to inspect what you get back. – nvoigt Sep 05 '14 at 06:08
  • @gus: I'm facing the same problem.Did you find any solution or alternative? – Syed Ali Taqi Jul 06 '15 at 07:09

1 Answers1

6

The documentation states that:

The MyComputer constant always yields the empty string ("") because no path is defined for the My Computer folder.

Supplying an empty string to the DirectoryInfo constructor is what causes the ArgumentException.

Elsewhere on MSDN you can find an explanation as to why an empty string is returned :

A folder will not physically exist if the operating system did not create it, the existing folder was deleted, or the folder is a virtual directory, such as My Computer, which does not correspond to a physical path.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
User 12345678
  • 7,714
  • 2
  • 28
  • 46