【问题标题】:How do you get the Default Users folder (e.g. c:\users\Default)如何获取默认用户文件夹(例如 c:\users\Default)
【发布时间】:2013-06-14 21:26:29
【问题描述】:

我查看了 Environment.GetFolderPath 方法和 System.Environment.SpecialFolder 枚举,但看不到任何返回 Default Users 文件夹路径的内容。

有人可以告诉我如何以编程方式获取默认用户文件夹(或者更好的是默认用户 AppData 本地文件夹路径,例如 c:\users\Default\AppData\Local),因为我需要将一些文件复制到此文件夹中?

谢谢

【问题讨论】:

    标签: c# .net


    【解决方案1】:

    网络上有很多文章描述了如何更改默认用户配置文件路径:

    http://support.microsoft.com/kb/214636

    http://www.nextofwindows.com/how-to-change-user-profile-default-location-in-windows-7/

    他们都说当前的默认配置文件路径存储在以下注册表位置:

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList
    

    例如 %SystemDrive%\Users\Default

    我找到了这个页面来获取系统驱动器: How to get current windows directory e.g. C:\ in C#

    Path.GetPathRoot(Environment.SystemDirectory)
    

    所以我将使用它。感谢您的帮助。

    更新

    我刚刚尝试了以下代码,它返回 C:\Users\Default。因此无需替换注册表项中存储的 %SystemDrive% 文本。它会自动替换它。

    using (RegistryKey profileListKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"))
    {
        string defaultPath = profileListKey.GetValue("Default").ToString();
    }
    

    【讨论】:

      【解决方案2】:

      来自LINQPad(语言:C# 程序)的片段输出'C:\Users\Default\Desktop':

      void Main()
      {
          GetFolderPath(Environment.SpecialFolder.Desktop).Dump();
      }
      
      // Define other methods and classes here
      [DllImport("shfolder.dll", CharSet=CharSet.Auto)]
      internal static extern int SHGetFolderPath(IntPtr hwndOwner, int nFolder, int hToken, int dwFlags, StringBuilder lpszPath);
      
      public static string GetFolderPath(Environment.SpecialFolder folder)
      {
          if (!Enum.IsDefined(typeof(Environment.SpecialFolder), folder))
          {
              throw new Exception("Crap");
          }
          StringBuilder lpszPath = new StringBuilder(260);
      
          SHGetFolderPath(IntPtr.Zero, (int) folder, -1, 0, lpszPath);
          string path = lpszPath.ToString();
          new FileIOPermission(FileIOPermissionAccess.PathDiscovery, path).Demand();
          return path;
      }
      

      编辑:我在 LINQPad 中有以下导入

      System.Runtime.InteropServices
      System.Globalization
      System.Security.Permissions
      

      我使用反射器查看Environment.GetFolderPath,然后查看SHGetFolderPath specifies,通过将 -1 作为 hToken 传递,您将获得默认用户。

      【讨论】:

      • SHGetFolderPath -- "为 hToken 参数赋值 -1 表示默认用户。这使 SHGetFolderPath 的客户端能够找到默认用户的文件夹位置(例如桌面文件夹)。默认用户创建任何新用户帐户时都会复制用户配置文件,其中包括特殊文件夹,例如我的文档和桌面。添加到默认用户文件夹的任何项目也会出现在任何新用户帐户中。 -- msdn.microsoft.com/en-us/library/windows/desktop/…
      • 我现在不建议使用 p/Invoke。
      • @JoanComasFdz 那么你会如何推荐它呢?
      • @JoanComasFdz,使用 pinvoke 并没有错,尤其是在您访问 .NET 框架未公开的功能时(公开的功能没有必要)。
      【解决方案3】:

      您不能因为对该文件夹的访问被拒绝,该文件夹仅供 Microsoft 使用。我确定 Environment 或任何其他类不会为您提供此类功能。这只能通过某种黑客攻击来完成?

      【讨论】:

      • 权限基于应用程序运行的上下文。管理员当然可以访问该文件夹。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-21
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 2018-10-20
      • 1970-01-01
      相关资源
      最近更新 更多