Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top