How to force my C# Winforms program run as administrator on any computer?

Gold picture Gold · Aug 30, 2010 · Viewed 50.8k times · Source

How to force my C# Winforms program run as administrator on any computer ? and any kind of OS ?

I need code solution (any sample code will be excellent)

Thanks in advance

Answer

Alex Reitbort picture Alex Reitbort · Aug 30, 2010

You can embed this manifest into your application.

<?xml version="1.0" encoding="utf-8" ?> 
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <assemblyIdentity version="1.0.0.0" name="MyApplication" />
    <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
        <security>
            <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
                <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
            </requestedPrivileges>
        </security>
    </trustInfo>
</asmv1:assembly>