简体   繁体   中英

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

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.

您想要DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)请参阅: SpecialFolder.Desktop和SpecialFolder.DesktopDirectory有什么区别?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM