How to get the Desktop's path?

1.1k Views Asked by At

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

There are 2 best solutions below

0
Daniel A. White On

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

0
Gabriel Luci On

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.