C# - Windows 7 - 创建文件

发布于 2024-11-07 12:59:13 字数 810 浏览 4 评论 0原文

基本上,我有一个安装在用户计算机上的应用程序。

用户使用的是 Windows 7。 用户没有获得管理员访问权限。 但是,应用程序需要能够将文件保存到其自己的 Program Files 目录中。

我想要采取的路径是:

  1. 下载各种二进制文件(网络服务)。
  2. 将二进制文件写入临时文件夹中的文件。
  3. 启动控制台应用程序。 (控制台应用程序等待主应用程序关闭)
  4. 控制台应用程序将临时文件复制到 Program Files 目录。
  5. 控制台应用程序重新启动主应用程序。
  6. 控制台应用程序关闭。

问题是我知道 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:

  1. Download various binaries (web service).
  2. Write binary to files in temporary folder.
  3. Launch a console app. (Console App waits for the main app to shutdown)
  4. Console App copies the temporary files to the Program Files directory.
  5. Console App relaunches the main app.
  6. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

じее 2024-11-14 12:59:13

正如评论已经指出的那样:如果您使用的是较新版本的 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.

面犯桃花 2024-11-14 12:59:13

一种解决方案是在安装过程中更改已安装文件夹的权限。

回声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

浮世清欢 2024-11-14 12:59:13

要了解 UAC 的工作原理,首先尝试使用术语“进程”而不是“应用程序”,并阅读以下内容:

  • 进程的权限在进程启动之前确定
  • 。从另一个进程生成的每个进程都会继承其安全性,或者:
    • 询问海拔

从这里你可以推断出第 3 步:

<前><代码>3。启动控制台应用程序。 (控制台应用程序等待主应用程序关闭)

将继承第一个运行的进程(您的应用程序)的权利。

有时您需要请求海拔高度。如果这是在您的应用程序运行之前或运行子进程之前,则由您选择。

最用户友好的方法是在首次启动或安装时修改文件夹权限。这是一种不会每次都打扰用户的方法。但某些 UAC 肯定会在某个时候向用户弹出。

To understand how the UAC works first try to use the term PROCESS instead of app and read this:

  • RIGHTS for a PROCESS are determined before the process starts
  • Every Process that is spawned from another inherits its security or:
    • Asks for elevation

From this you can deduce that step 3:

3. Launch a console app. (Console App waits for the main app to shutdown)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文