在 C# 中设置文件的 UAC 设置
我想授予文件(已存在于客户端计算机上的 .exe)权限,使其始终以管理权限执行。
请注意,我想要授予权限的文件已经在目标计算机上。而且,我想通过另一个用 C# 编写的程序更改该文件的权限,并且它具有执行所有操作的管理权限。
请告诉我该怎么做,我正在使用这段代码:
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);
此代码将正确更改权限,但在执行此代码后执行 inam.exe
时仍然如此。 UAC未出现,inam.exe
也无法执行管理操作。
事实上,我已经在超过10,000个客户端上部署了一个应用程序,所以想发布一个补丁来解决管理权限问题。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用管理权限执行不是文件权限。
这通常是通过添加 清单文件(添加到 Win32 EXE 中的资源,或作为外部清单)。该清单文件可以说明应用程序是否需要以提升的权限运行。
我不完全确定 Windows 将“以管理员身份运行此程序”兼容性设置隐藏在哪里。
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.
使用清单文件是最好的方法,但另一种方法是以编程方式设置“以管理员身份运行此程序”标志(您可以在 EXE 属性的“兼容性”选项卡中找到该选项),通过设置一个简单的注册表项。您需要在这些键之一下创建一个字符串值 (
REG_SZ
)(如果您希望设置分别针对每个用户或每台计算机):或者
值的名称需要是可执行文件的完整路径(如果路径包含空格,请勿用引号将路径引起来),并且值的数据必须包含字符串
RUNASADMIN
。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):or
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
.构建清单文件(请参阅 http://www.gregcons.com/KateBlog/AddingAManifestToAVistaApplication.aspx< /a> 等地方)并将其命名为 Whatever.exe.manifest 并将其放在与 exe 相同的文件夹中。 naifest 应将requestedExecutionLevel 设置为requireAdministrator。一切就绪。
如果您拥有另一个 exe,则可以在构建它时嵌入清单。这在 Visual Studio 2008 及更高版本中几乎是微不足道的。查看“应用程序”选项卡并下拉“清单”下拉列表。附近有说明。此外,当您使用 VS 2008 向项目添加清单时,您不必键入所有 XML,只需从为您生成的注释中复制适当的请求执行级别即可。
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.