Get desktop path for all administrators in C#

感情迁移 提交于 2019-12-10 16:03:14

问题


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.


回答1:


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.

No idea how difficult that will be though.




回答2:


There are a few options that you can go with, depending on how you want to do it.

Option A:

Hard coded, but it works for default system setups

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

Option 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!



来源:https://stackoverflow.com/questions/8923618/get-desktop-path-for-all-administrators-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!