views:

365

answers:

3

Hi guys.

i want to give a file(already present on the client computer .exe) permissions to always execute with administrative permissions.

please note that the file i wants to give permissions is already on target machine. and i want to change permissions of that file through another program written in c# and it has administrative permissions to do everything.

kindly let me know how to do it i am using this code

        System.Security.AccessControl.FileSecurity fs = File.GetAccessControl(@"c:\inam.exe");
        FileSystemAccessRule fsar = new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, AccessControlType.Allow);
        fs.AddAccessRule(fsar);
        File.SetAccessControl(@"c:\inam.exe", fs);

this code will change the permissions correctly but still when i execute inam.exe after executing this code the UAC not appeared and also the inam.exe cant perform administrative operations.

actually i have already deployed an application on more than 10,000 clients so wants to release a patch to resolve the administrative rights issue.

+3  A: 

Execute with administrative privileges is not a file permission.

This is usually configured by adding a manifest file (either to the Win32 resources in the EXE, or as an external manifest). This manifest file can state whether the application needs to run elevated or not.

I'm not entirely sure where Windows stashes the "Run this program as an administrator" compatibility setting.

Roger Lipscombe
+1  A: 

Build a manifest file (see http://www.gregcons.com/KateBlog/AddingAManifestToAVistaApplication.aspx among other places) and name it Whatever.exe.manifest and put it in the same folder as the exe. The nanifest should set the requestedExecutionLevel to requireAdministrator. All set.

If you own the other exe, you can embed the manifest when you build it. This is almost trivial in Visual Studio 2008 and up. See the Application tab and drop down the Manifests drop down. There are instructions nearby. Also when you use VS 2008 to add a manifest to your project you don't have to type all the XML, you just copy the appropriate requested execution level from the comments that are generated for you.

Kate Gregory
Thanks man.Problem resolved.
Inam Jameel
+2  A: 

Using a manifest file is the best approach, but an alternative one would be to programmatically set the "Run this program as an administrator" flag (the option you find in the Compatibility tab of an EXE's properties), by setting a simple registry key. You need to create a string value (REG_SZ) under the one of these keys (if you want the setting to be per user or per machine, respectively):

HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers

or

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers

The name of the value needs to be the full path to your executable (if the path contains spaces, do not surround the path with quotes), and the data of the value must contain the string RUNASADMIN.

Allon Guralnek