Hello

Im making a application which needs some administrator priviliges. The thing is that this besides 7 will also run on XP.

Ive been told that changing in the app.manifest the requestedExecutionLevel is one step as this basically will require a admin

<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
        <!-- UAC Manifest Options
            If you want to change the Windows User Account Control level replace the 
            requestedExecutionLevel node with one of the following.

        <requestedExecutionLevel  level="asInvoker" uiAccess="false" />
        <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
        <requestedExecutionLevel  level="highestAvailable" uiAccess="false" />

            Specifying requestedExecutionLevel node will disable file and registry virtualization.
            If you want to utilize File and Registry Virtualization for backward 
            compatibility then delete the requestedExecutionLevel node.
        -->
        <requestedExecutionLevel level="highestAvailable" uiAccess="false" />
      </requestedPrivileges>

But the other problem occures in the legacy world: On XP, ive been told this basically means nothing and I have to use a "IsInRole" method. Currently I have this:

static void Main()
{
// AppDomain myDomain = Thread.GetDomain();
// myDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);

        WindowsPrincipal myPrincipal = (WindowsPrincipal) Thread.CurrentPrincipal;
        if (myPrincipal.IsInRole(WindowsBuiltInRole.User))
        {
            MessageBox.Show("You must be admin to run this");
        }
        else
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new FormPrincipal());  
        }



    }

But it doesnt work (this is in the Program.cs). It simply doesnt allow me to run the program (7 or XP; Simply doesnt let me run and shows the message in the MessageBox).

Any tips?

Thanks!

I couldn't get that to run. Kept throwing an invalid cast exception when casting Thread.CurrentPrincipal. I did, however, get this to work:

        var x = new WindowsPrincipal(WindowsIdentity.GetCurrent());
        if (x.IsInRole(WindowsBuiltInRole.Administrator))
            //You are an admin
        else
            //Not an admin

I couldn't get that to run. Kept throwing an invalid cast exception when casting Thread.CurrentPrincipal. I did, however, get this to work: var x = new WindowsPrincipal(WindowsIdentity.GetCurrent()); if (x.IsInRole(WindowsBuiltInRole.Administrator)) //You are an admin else //Not an admin

Thanks. Im going to try that out and see if I can get it to work...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.