问题
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