JustForCodes87
JustForCodes87

Reputation: 19

How to get the Desktop's path?

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();

Upvotes: 1

Views: 1521

Answers (2)

Gabriel Luci
Gabriel Luci

Reputation: 41008

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.

Upvotes: 5

Daniel A. White
Daniel A. White

Reputation: 191058

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

Upvotes: 1

Related Questions