0

I am trying to create a folder in the Desktop (Using DirectoryInfo) - I need to get the Desktop path

I've tried using:

DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)

But it keeps getting me into the user's Folder (Where the Desktop, Music, Vidoes folders are).

DirectoryInfo dir = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "Folder111" );
dir.Create();
Emma
  • 26,487
  • 10
  • 35
  • 65

2 Answers2

4

You aren't formatting the path correctly. You're just tacking on the new folder name to the desktop folder name. So if the desktop folder is at C:\Users\MyUsername\Desktop, you are creating a folder called C:\Users\MyUsername\DesktopFolder111, when what you really want is C:\Users\MyUsername\Desktop\Folder111 (you're missing the slash).

Use Path.Combine() to automatically add the slash for you:

DirectoryInfo dir = new DirectoryInfo(
    Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Folder111"));

Daniel's answer may also be applicable.

Gabriel Luci
  • 33,807
  • 4
  • 44
  • 75
1

You want DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) See: What's the difference between SpecialFolder.Desktop and SpecialFolder.DesktopDirectory?

Daniel A. White
  • 181,601
  • 45
  • 354
  • 430