如何在 C# 中为当前用户获取桌面路径?

Posted

技术标签:

【中文标题】如何在 C# 中为当前用户获取桌面路径?【英文标题】:How to get a path to the desktop for current user in C#? 【发布时间】:2010-10-12 15:48:52 【问题描述】:

我唯一能找到的是 VB.NET-only 类 SpecialDirectories,它具有以下属性:

My.Computer.FileSystem.SpecialDirectories.Desktop

如何在 C# 中做到这一点?

【问题讨论】:

【参考方案1】:
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

【讨论】:

从该文件夹返回的项目与 Window Explorer 显示的不同。例如。在我的 XP 中,它不包括我的文档、我的电脑、我的网上邻居、回收站和其他一些快捷方式。知道如何获得与 Windows Explorer 相同的条目吗? 也许您正在寻找 SpecialFolder.DesktopDirectory?这是物理文件夹而不是逻辑文件夹。 如果程序以管理员身份运行,这将返回管理员用户桌面 @Emma 我记得 - 没问题【参考方案2】:
// Environment.GetFolderPath
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); // Current User's Application Data
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData); // All User's Application Data
Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles); // Program Files
Environment.GetFolderPath(Environment.SpecialFolder.Cookies); // Internet Cookie
Environment.GetFolderPath(Environment.SpecialFolder.Desktop); // Logical Desktop
Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); // Physical Desktop
Environment.GetFolderPath(Environment.SpecialFolder.Favorites); // Favorites
Environment.GetFolderPath(Environment.SpecialFolder.History); // Internet History
Environment.GetFolderPath(Environment.SpecialFolder.InternetCache); // Internet Cache
Environment.GetFolderPath(Environment.SpecialFolder.MyComputer); // "My Computer" Folder
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); // "My Documents" Folder
Environment.GetFolderPath(Environment.SpecialFolder.MyMusic); // "My Music" Folder
Environment.GetFolderPath(Environment.SpecialFolder.MyPictures); // "My Pictures" Folder
Environment.GetFolderPath(Environment.SpecialFolder.Personal); // "My Document" Folder
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles); // Program files Folder
Environment.GetFolderPath(Environment.SpecialFolder.Programs); // Programs Folder
Environment.GetFolderPath(Environment.SpecialFolder.Recent); // Recent Folder
Environment.GetFolderPath(Environment.SpecialFolder.SendTo); // "Sent to" Folder
Environment.GetFolderPath(Environment.SpecialFolder.StartMenu); // Start Menu
Environment.GetFolderPath(Environment.SpecialFolder.Startup); // Startup
Environment.GetFolderPath(Environment.SpecialFolder.System); // System Folder
Environment.GetFolderPath(Environment.SpecialFolder.Templates); // Document Templates

【讨论】:

以上是关于如何在 C# 中为当前用户获取桌面路径?的主要内容,如果未能解决你的问题,请参考以下文章

在 c# 中为使用 AzureAd 登录的当前用户获取 Jwt 令牌

如何获取当前工作目录路径 c#?

C#中如何获得当前应用程序路径?

VC++如何获取并保存桌面背景?

获取我的应用程序的当前路径 [重复]

c#中怎么获得当前路径