以用户身份运行我的程序
Windows 7、Vista、Server 2008、UAC 已激活
程序必须具有管理员权限才能进行某些安装操作。之后,我希望我的程序能够继续以非管理员权限运行。
如何在没有管理权限的情况下重新启动它?
PS
我的程序会自行重新安装。我不想为其分发任何附加程序。所以我的步骤是:
- 在临时目录中下载新版本
- 在管理员权限下重新启动
- 重命名旧的 exe 文件并从临时目录复制新的 exe 文件dir
- 在非管理员权限下自行重启
Windows 7, Vista, Server 2008, UAC is activated
Program must be stated with admin rights to make some installation actions. After that I want my program to continue work with non-admin rights.
How can I restart it with not administrative rights?
P.S.
My program reinstall itself. I don't want distribute any additional programs for it. So my steps are:
- Download new version in temp dir
- Restart itself under admin rights
- Rename old exe-file and copy new exe-file from temp dir
- Restart itself under non-admin rights
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 UAC 下,现在强烈建议不要在“首次运行”时执行任何操作。此外,使用自行开发技术进行自我更新的程序会发现更加困难。您说您不想分发额外的程序,但在 UAC 下您实际上别无选择。要么你的整个应用程序每次都以提升的方式运行(惹恼用户),以防它碰巧需要执行一些管理操作,要么将其分成两部分,并偶尔运行一个提升的部分,而另一部分始终不提升。
拆分它的一种方法是编写一个安装程序,它可以提升,而常规应用程序则不能。这适用于安装一次、在第一次运行时执行一些操作(将这些操作移至安装程序)然后完成的人。你说你的应用程序会自行更新。因此,您需要将该代码移动到一个单独的 exe 中,并在该 exe 上放置一个包含 requireAdministrator 的清单。然后,当有新的更新可用时,您的主应用程序将启动(使用 ShellExecute)更新 exe。
Under UAC, doing anything "on first run" is now strongly discouraged. Also, programs that update themselves using a roll-your-own technique will find it more difficult. You say you don't want to distribute additional programs, but under UAC you really have very little choice. Either your whole app runs elevated every time (annoying the user) in case it happens to need to do something administrative, or you split it into two parts, and run one elevated occasionally and the other non elevated all the time.
One way to split it is to write an installer, which elevates, and the regular app, which doesn't. That works for the people who install once, do some things on first run (you move those things to the installer) and then are done. You say your app updates itself. So you need to move that code to a separate exe and put a manifest on that exe that has requireAdministrator. Then your main app will launch (using ShellExecute) the updating exe when there is a new update available.
感谢凯特·格雷戈里的帮助。
Delphi 上有一个工作代码:
Thanx to Kate Gregory for help.
There is a working code on Delphi:
我认为你在这方面走错了路。我认为您应该执行以下操作之一:
,或者
编辑:
因此,步骤如下:
请求提升无需重新启动。在 Vista 之前的环境中工作时,您可能仍希望使用这种方式。
I think you are going the wrong way at this. In my opinion you should do one of the following:
or
Edit:
So the steps would be:
There is no restart necessary for requesting elevation. You might want to still use this way when working on pre-Vista environments.
这是一个简单的重启方法;
Here's a simple restart method;