Search code examples
c#permissionsuacpatchadministrative

setting UAC settings of a file in C#


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, also the inam.exe can't 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.


Solution

  • 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.