C# - Windows 7 - 创建文件
基本上,我有一个安装在用户计算机上的应用程序。
用户使用的是 Windows 7。 用户没有获得管理员访问权限。 但是,应用程序需要能够将文件保存到其自己的 Program Files 目录中。
我想要采取的路径是:
- 下载各种二进制文件(网络服务)。
- 将二进制文件写入临时文件夹中的文件。
- 启动控制台应用程序。 (控制台应用程序等待主应用程序关闭)
- 控制台应用程序将临时文件复制到 Program Files 目录。
- 控制台应用程序重新启动主应用程序。
- 控制台应用程序关闭。
问题是我知道 Windows 7 不允许不以管理员身份运行的应用程序写入 Program Files 目录,并且我理解原因(为了安全),但由于我自己编写这个应用程序,所以自己将其安装在计算机上,有什么方法可以让我的应用程序能够写入它所在的任何目录(平台独立,因为它使用相对路径),而不会出现弹出框要求以管理员身份运行应用程序?无法将应用程序签名为始终以管理员身份运行吗?
事实上,我什至不需要主应用程序是通过管理访问权限运行的应用程序。我需要控制台应用程序(复制临时文件的应用程序)能够将这些临时文件复制为永久文件。
更新:是的,这是一个自动更新的应用程序。我考虑过 ClickOnce 等,但还有一些额外的要求导致我创建自己的内部更新,主要是因为更新需要静默且逐步进行。有时(取决于更新的部分)应用程序需要关闭、移入文件、重新启动。其他时候,应用程序只需要移入文件并继续运行。
ClickOnce 不适合我的情况,我们的组织正在寻找内部的东西,以便可以对其进行定制以满足我们未来的需求。
Basically, I have an application that is installed on the users computers.
The users are on Windows 7.
The users are NOT given admin access.
The application, however, needs to be able to save files to its own Program Files directory.
The path I wanted to take was:
- Download various binaries (web service).
- Write binary to files in temporary folder.
- Launch a console app. (Console App waits for the main app to shutdown)
- Console App copies the temporary files to the Program Files directory.
- Console App relaunches the main app.
- Console App shuts down.
The problem is that I know Windows 7 does not allow applications that are not running as administrator to write to the Program Files directory, and I understand why (for security), but since I am writing this app myself, installing it on the machines myself, is there any way to make my app be able to write to whichever directory it resides in (platform independent because it uses relative paths) without having a popup box ask to run the app as admin? Can't the app be signed to ALWAYS run as admin?
In fact, I don't even need the main application to be the one that runs with administrative access. I need the console-app (the one that copies the temporary files) to be able to copy those temporary files as permanent files.
Update: Yes, this is for an auto-updating application. I thought about ClickOnce and the such, but there are additional requirements which lead me to create my own internal updating, mainly because the updates need to be silent and piece by piece. Sometimes (depending on the pieces updated) the application needs to shutdown, move the files in, restart. Other times the application simply needs to move the files in and continue running.
ClickOnce just didn't work for my situation, and our organization was looking for something in-house so it can be customized to fit our future needs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如评论已经指出的那样:如果您使用的是较新版本的 Windows (Vista+)、启用了 UAC(默认)且非管理员用户,则 ProgramFiles 是无法访问的。
您更新的问题表明您需要更新(至少部分)您的应用程序,并且可能需要重新启动。您创建了自己的模块更新方式。
我的建议如下: 不要写入 ProgramFiles
要么将应用程序完全安装到用户配置文件中,要么将其拆分。
我尝试创建一个功能非常小的可执行文件:
设置遮蔽 以便程序集不被锁定
在可写位置(ProgramData 或用户配置文件)查找程序集并加载它
从那里运行应用程序
如果发生更新,您可以覆盖您的程序集(因为它们被隐藏并存储在合理的位置) ),如有必要,停止程序/要求用户重新启动/实施重新启动机制。为此,您不需要管理权限。
As the comments already pointed out: ProgramFiles is inaccessible if you have a somewhat recent version of Windows (Vista+), UAC enabled (the default) and non-admin users.
Your updated question says that you need to update (at least parts of) your application and that might need a restart. You created your own way to update the modules.
My suggestion is the following: Don't write to ProgramFiles
Either install your application completely to the user profile or split it up.
I'd try to create an executable that does very litte:
Sets up shadowing so that assemblies are not locked
Look up an assembly in a writable location (ProgramData or in the user profile) and load it
Run the app from there
In case of an update you can overwrite your assemblies (since they are shadowed and stored in a sensible location) and, if necessary, stop the program/ask the user to relaunch/implement a restart mechanism. You shouldn't need administrative privileges for this.
一种解决方案是在安装过程中更改已安装文件夹的权限。
回声y| cacls /E /T /P 用户:F
One solution would be to change the installed folder's permission during installation.
echo y| cacls /E /T /P Users:F
要了解 UAC 的工作原理,首先尝试使用术语“进程”而不是“应用程序”,并阅读以下内容:
从这里你可以推断出第 3 步:
将继承第一个运行的进程(您的应用程序)的权利。
有时您需要请求海拔高度。如果这是在您的应用程序运行之前或运行子进程之前,则由您选择。
最用户友好的方法是在首次启动或安装时修改文件夹权限。这是一种不会每次都打扰用户的方法。但某些 UAC 肯定会在某个时候向用户弹出。
To understand how the UAC works first try to use the term PROCESS instead of app and read this:
From this you can deduce that step 3:
Will inherit the rights of the first process that was run (your app).
At some point you will need to ask for elevation. If that is before your app is run or before running asubprocess, is your choice.
The most user friendly way to do this is to modify folder permissions once at first start or installation. That is a way to not bother the user each time. But some UAC will surely pop to the user at some point.