简体   繁体   English

想要用Win7修改Program Files下的文件

[英]want to modify a file under Program Files with Win7

I have a small C# program to modify a xml file which is located under Program Files . 我有一个小的C#程序来修改位于Program Files下的xml文件。 The machine is a Windows 7 machine. 该计算机是Windows 7计算机。 This small program is launched by a batch file (called A.bat ) because I want to pass a parameter to it. 这个小程序由批处理文件(称为A.bat )启动,因为我想向其传递参数。

I have a master batch file (called M.bat ) which is the start point. 我有一个主批处理文件(称为M.bat ),这是起点。 The M.bat will start an installer and wait untile the installation finished. M.bat将启动安装程序,并等待安装完成。 Then the M.bat will start A.bat which will launch my small program with a parameter. 然后M.bat将启动A.bat ,这将启动带有参数的我的小程序。

Right now I get the following exception: 现在,我得到以下异常:

System.UnauthorizedAccessException: Access to the path 'C:\Program Files\MyTest\Test.config' is denied.

I know it is caused by tighter security in Win7. 我知道这是Win7中更严格的安全性引起的。 It works fine under XP. 在XP下工作正常。

I cannot use "Run as Administrator" to start M.bat or manually "Run as Administrator" to start A.bat because both will report cannot find the target executable (because the start location is not really the "current" location then). 我不能使用“以管理员身份运行”来启动M.bat或手动使用“以管理员M.bat运行”来启动A.bat因为两者都会报告找不到目标可执行文件(因为起始位置实际上不是“当前”位置)。

Is there a way to start an executable as Administrator in batch file? 有没有办法在批处理文件中以管理员身份启动可执行文件? or in C# program I can give myself Administrator right? 还是在C#程序中我可以给自己管理员权限?

or ... 要么 ...

Not in a way that is invisible/hidden from the user... I would suggest finding a way to make it work when run as an administrator. 并非以用户看不见/隐藏的方式...我建议您找到一种以管理员身份运行时使其工作的方式。 Or you could set the application manifest (see this: http://www.enusbaum.com/blog/2007/08/26/how-to-run-your-c-application-as-administrator-in-windows-vista/ ) to run your app as admin, that might work as well. 或者,您也可以设置应用程序清单(请参见: http : //www.enusbaum.com/blog/2007/08/26/how-to-run-your-c-application-as-administrator-in-windows-vista / )以管理员身份运行您的应用,这也许也可以。

Bottom line, you cannot run with admin privileges unless you run as admin, or unless your user switches off UAC (which is not recommended at all). 最重要的是,除非您以admin身份运行,或者除非您的用户关闭了UAC(完全不建议这样做),否则您不能使用admin特权运行。

You'll need to elevated your privileges through a UAC prompt. 您需要通过UAC提示符提升特权。 Add a manifest to your program as described in this answer . 按照此答案中的说明将清单添加到程序中。

You should request for admin privileges at program start up. 您应该在程序启动时请求管理员权限。 Look at this sample 这个样本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM