想要用Win7修改Program Files下的文件
我有一个小的 C# 程序来修改位于 Program Files
下的 xml 文件。该机器是 Windows 7
机器。这个小程序由批处理文件(称为 A.bat
)启动,因为我想向它传递一个参数。
我有一个主批处理文件(称为 M.bat
),它是起点。 M.bat
将启动安装程序并等待安装完成。然后M.bat
将启动A.bat
,它将启动带有参数的小程序。
现在我得到以下异常:
System.UnauthorizedAccessException: Access to the path 'C:\Program Files\MyTest\Test.config' is denied.
我知道这是由于Win7中更严格的安全性引起的。 XP下运行正常。
我无法使用“以管理员身份运行”来启动 M.bat
或手动“以管理员身份运行”来启动 A.bat
,因为两者都会报告找不到目标可执行文件(因为那么起始位置实际上并不是“当前”位置)。
有没有办法在批处理文件中以管理员身份启动可执行文件?或者在 C# 程序中我可以给自己设置管理员权限吗?
或者 ...
I have a small C# program to modify a xml file which is located under Program Files
. The machine is a Windows 7
machine. This small program is launched by a batch file (called A.bat
) because I want to pass a parameter to it.
I have a master batch file (called M.bat
) which is the start point. The M.bat
will start an installer and wait untile the installation finished. Then the M.bat
will start A.bat
which will launch my small program with a parameter.
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. It works fine under 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).
Is there a way to start an executable as Administrator in batch file? or in C# program I can give myself Administrator right?
or ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不是以一种对用户不可见/隐藏的方式...我建议找到一种方法使其在以管理员身份运行时正常工作。或者您可以设置应用程序清单(请参阅:http://www.enusbaum.com/blog/2007/08/26/how-to-run-your-c-application-as-administrator-in-windows -vista/)以管理员身份运行您的应用程序,这也可能有效。
最重要的是,除非您以管理员身份运行,或者除非您的用户关闭 UAC(根本不建议这样做),否则您无法以管理员权限运行。
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.
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).
您需要通过 UAC 提示提升您的权限。将清单添加到您的程序中,如 这个答案。
You'll need to elevated your privileges through a UAC prompt. 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