简体   繁体   English

C#-Windows 7-在以下位置创建文件

[英]C# - Windows 7 - Create file in

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. 用户在Windows 7上。未向用户授予管理员访问权限。 The application, however, needs to be able to save files to its own Program Files directory. 但是,该应用程序需要能够将文件保存到其自己的Program Files目录中。

The path I wanted to take was: 我想走的路是:

  1. Download various binaries (web service). 下载各种二进制文件(Web服务)。
  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. Console App将临时文件复制到Program Files目录。
  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? 问题是,我知道Windows 7不允许未以管理员身份运行的应用程序写入Program Files目录,并且我理解原因(出于安全性考虑),但是由于我自己编写此应用程序,因此请自己在计算机上安装它,是否有任何方法可以使我的应用程序能够写到它所在的任何目录(与平台无关,因为它使用相对路径),而无需弹出框要求以管理员身份运行该应用程序? 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. 我需要console-app(用于复制临时文件的应用程序)才能将这些临时文件复制为永久文件。


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. 我曾考虑过ClickOnce之类的东西,但是还有其他一些要求使我创建自己的内部更新,这主要是因为更新需要保持沉默和逐段。 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. ClickOnce不适用于我的情况,我们的组织正在内部寻找某种东西,因此可以对其进行自定义以满足我们的未来需求。

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. 正如评论所指出的那样:如果您使用的Windows版本比较新(Vista +),启用了UAC(默认)和非管理员用户,则无法访问ProgramFiles。

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 我的建议如下:不要写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 在可写位置(ProgramData或用户配置文件中)查找程序集并将其加载

  • 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. 为此,您不需要管理特权。

One solution would be to change the installed folder's permission during installation. 一种解决方案是在安装过程中更改已安装文件夹的权限。

echo y| 回声y | cacls /E /T /P Users:F cacls / E / T / P用户:F

To understand how the UAC works first try to use the term PROCESS instead of app and read this: 要了解UAC的工作原理,请首先尝试使用术语PROCESS而不是app并阅读以下内容:

  • 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:

 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. 但是某些UAC肯定会在某个时候弹出给用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM