4

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.

mxpv
  • 946
  • 2
  • 10
  • 31
  • Do you need to iterate all user on the machine in advance, or can you dynamically create the shortcut when as user logs in? – Myles McDonnell Jan 19 '12 at 09:26
  • What happens if someone is added to the Administrators group after the app is installed? When is their shortcut created? Instead, I would add a shortcut to the public desktop folder (so all users see it) but use a manifest to require administrative permission to run it (assuming Vista or later). – anton.burger Jan 19 '12 at 09:42
  • Not dynamically, just iterate and create shortcuts. – mxpv Jan 19 '12 at 09:51

2 Answers2

0

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.

ICR
  • 13,896
  • 4
  • 50
  • 78
  • I'm just googling this function and trying to find how to obtain user token. – mxpv Jan 19 '12 at 10:35
  • 2
    The only way I know to get a user token _without_ a password or an existing impersonation context is to call the LsaLogonUser function. For good reasons, you have to have SeTcbPrivilege ("Act as part of the operating system") for this, which by default means running as LocalSystem (think very carefully before deciding to go down this route). Note that SHGetKnownFolderPath and its older cousins also specify that the user's registry hive must be loaded, which implies calling LoadUserProfile, another operation which requires a token. I really would go with a public shortcut and admin manifest. – anton.burger Jan 19 '12 at 10:59
-3

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!

Borisonekenobi
  • 469
  • 4
  • 15
Mitchel Sellers
  • 62,228
  • 14
  • 110
  • 173