【问题标题】:Get all Environment.SpecialFolder.ApplicationData in C# .NET [duplicate]在 C# .NET 中获取所有 Environment.SpecialFolder.ApplicationData [重复]
【发布时间】:2013-10-24 17:33:51
【问题描述】:

我想从安装为具有管理员权限的本地系统的 Windows 服务中获取所有用户的 Environment.SpecialFolder.ApplicationData。

例子:

如果我有 Tom、Matt 和 Christine 用户,我需要:

C:\Users\Matt\AppData\Local

C:\Users\Tom\AppData\Local

C:\Users\Christine\AppData\Local

提前致谢!

【问题讨论】:

标签: c#


【解决方案1】:

下面是代码,您可以通过它找到任何用户的 ApplicationData 路径 -

  1. 先找出当前用户的名字。
  2. 找出应用程序数据路径。
  3. 现在替换应用程序数据路径中的当前用户名。

    /// <summary>
    /// GetAppDatafolder
    /// </summary>
    private static void GetAppDatafolder(string otherUserName)
    {
        var currentUserIdentity = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
        var currentUserName = (currentUserIdentity.Contains(@"\")) ? (currentUserIdentity.Split('\\')[1]) : currentUserIdentity;
        var currentAppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
        var otherUserAppDataPath = currentAppDataPath.Replace(currentUserName, otherUserName);
    
        Console.WriteLine(string.Format("Current User AppDataPath : {0}", currentAppDataPath));
        Console.WriteLine(string.Format("{0} AppDataPath : {1}", otherUserName, otherUserAppDataPath));
    }
    

【讨论】:

    猜你喜欢
    • 2017-05-16
    • 2013-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-04
    相关资源
    最近更新 更多