我在 Vista/7 上遇到 UAC 问题。
我的 UAC 执行级别 requireAdministrator 安装程序安装并运行我的应用程序。
因此,安装程序第一次运行应用程序时,它会以管理员身份运行。然后,应用程序创建一些文件来存储自身状态。
稍后(如果应用程序由用户启动,而不是以管理员身份启动)应用程序只能读取创建的文件,但不能覆盖它们。
我尝试在应用程序清单中禁用 UAC,或者在没有清单的情况下构建应用程序,但结果仍然相同 - 当应用程序以用户身份运行时,以管理员身份运行应用程序时创建的每个文件都不能被覆盖。我尝试了一些其他安装程序,例如 Inno Setup,但没有什么像我的问题一样......所以我的问题 - 为什么它发生在我的情况以及如何修复它?谢谢。
PS 重要提示
- 实际上它不是安装程序。这是更新主应用程序可执行文件的实用程序。主应用程序检查服务器是否有更新(如果有)——将更新下载到临时文件夹,然后以提升的权限启动实用程序 (http://www.codeproject.com/KB/vista-security/UAC__The_Definitive_Guide.aspx) 以替换程序文件中的可执行文件 文件夹。主应用程序在启动实用程序后立即终止。
- 应用程序的所有文件都存储在 ProgramData\myAppName 文件夹中。
I have a problem with UAC on Vista/7.
My installer with UAC Execution Level requireAdministrator installs and runs my app.
- App UAC Execution Level: asInvoker
So first time app runned by installer, it runs as administrator. Then, app creates some files to store self state.
Later (if app launched by user, not as administrator) app can only read from created files, but can't overwrite them.
I tried to disable UAC in the app manifest, or build app without manifest but result still same -- every file created when app runned as administrator can't be overwritten when app runs as user. I tried some other installers like Inno Setup but there nothing like my problem... So my question -- why it happens in my case and how to fix it ? Thanks.
P.S. Important notes
- Actually it is not installer. This is utility to update main app executables. Main app check server for updates, if any available -- downloads updates to the temp folder and then launch utility with elevated rights (http://www.codeproject.com/KB/vista-security/UAC__The_Definitive_Guide.aspx) in order to replace executables in the Program Files folder. Main app terminates just after launching utility.
- All files application stores in the ProgramData\myAppName folder.
发布评论
评论(2)
您安装的应用程序是仅针对当前用户还是针对计算机上的所有用户?
对于每用户安装,安装程序在用户的 AppData 文件夹中创建初始应用程序状态是有意义的。 MSI 以非提升方式运行每用户安装,因此应用程序无法修改的 AppData 文件夹中的文件没有问题。
对于每台计算机安装,将任何内容放入特定用户的 AppData 文件夹或用户配置文件位置下的任何位置是没有意义的。新用户可以在安装应用程序后登录计算机,并且他们不会有任何东西。
也就是说,您有三种解决方案(实际上是两种解决方案和一种 hack):
(#3) 是一种黑客行为,我不推荐它,因为您的应用程序对于新用户或用户在计算机上重新创建其个人资料时会被破坏。
Are your installing application for current user only or for all users on the machine?
For per-user installation it makes sense for the installer to create initial application state in the user's AppData folder. MSI runs per-user installations NON-elevated, so there is no problem with files in AppData folder your applications cannot modify.
For per-machine installation it does not make sense to put anything to the specific user's AppData folder or anywhere under users profile location. New users can logon on the machine after application is installed and they would not have anything there.
That said, you have three solutions (actually two solutions and one hack):
(#3) is a hack and I don't recommend it, because your application would be broken for new users, or when user re-creates his profile on machine.
如果不需要管理员权限,则不应以提升的方式启动主应用程序。您使用什么类型的安装程序?
MSI 以当前用户权限运行,并且仅在对系统进行更改时进行提升。安装完成后,您可以启动主应用程序,它将以当前用户身份运行,而不是提升权限。
如果您自定义 setup.exe,则可以以非提升方式启动安装程序。当需要安装文件时,启动另一个进程(可以是相同的可执行文件)来安装文件并进行系统范围的更改。完成此步骤后,您可以从初始进程中以非提升状态重新启动主应用程序。
或者,使用当前安装程序和更新程序运行提升的方法,请按照 如何从桌面用户身份启动程序提升的应用启动您的主应用非提升。
注意:ProgramData 的默认权限允许每个人创建文件和文件夹,但只有创建它们的用户可以修改文件(或管理员)。为了克服这个问题,您有两个选择:
You should not start your main app elevated if it doesn't require administrator rights. What type of installer do you use?
MSI runs with current user privileges and elevates only to make changes to the system. After installation completes, you can start your main app and it will be run as the current user, non-elevated.
If you custom setup.exe, you can start your setup program non-elevated. When the time comes to install files, start another process (it can be the same executable) to install files and make system-wide changes. When this step completes, you can re-start your main app as non-elevated from the initial process.
Alternatively, with your current approach where setup and updater run elevated, follow the instructions at How do I start a program as the desktop user from an elevated app to start your main app non-elevated.
Note: the default permissions on ProgramData allow everyone to create files and folders but only the user that created them can modify the files (or administrator). To overcome this, you have two options: