简体   繁体   English

获取C#中所有管理员的桌面路径

[英]Get desktop path for all administrators in C#

I need to create desktop shortcuts to my app for all administratos in the system. 我需要为系统中的所有管理员创建应用程序的桌面快捷方式。 I'm using the following code to get user list. 我正在使用以下代码来获取用户列表。

        var identifier = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null);
        GroupPrincipal group = GroupPrincipal.FindByIdentity(new PrincipalContext(ContextType.Machine), identifier.Value);

        foreach (Principal principal in group.Members)
        {
            Console.WriteLine(principal.Name);
        }

I need somehow to get desktop path for each user. 我需要某种方式来获取每个用户的桌面路径。 Could you suggest me solution? 你能建议我解决办法吗? Many thanks. 非常感谢。

You'll want to pinvoke the SHGetFolderLocation function (http://msdn.microsoft.com/en-us/library/bb762180.aspx) which allows you to pass in an access token that represents the user you're interested in. 您将希望使用SHGetFolderLocation函数(http://msdn.microsoft.com/zh-cn/library/bb762180.aspx),该函数允许您传递表示您感兴趣的用户的访问令牌。

No idea how difficult that will be though. 不知道那会有多困难。

There are a few options that you can go with, depending on how you want to do it. 您可以选择几种方法,具体取决于您要执行的操作。

Option A: 选项A:

Hard coded, but it works for default system setups 硬编码,但适用于默认系统设置

var userDirectory = Path.Combine("C:\Users\", principal.Name, "\Desktop");

Option B: 选项B:

Find for the current user, then swap it out 查找当前用户,然后换出

var currentUser = Environment.GetFolderPath(Environment.SpecialFolder.Desktop));
var newUser = currentUser.Replace("MyUser", principal.Name);

Now, option B hasn't been fully tested, but should work! 现在,选项B尚未经过全面测试,但应该可以使用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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