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

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

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

My.Computer.FileSystem.SpecialDirectories.Desktop

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

【问题讨论】:

    标签: c# windows filesystems directory


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 2014-12-18
      • 2016-05-03
      • 2014-06-09
      • 1970-01-01
      • 2012-06-15
      • 2018-09-02
      • 1970-01-01
      • 2018-07-22
      相关资源
      最近更新 更多