开发者

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. 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 thou开发者_Go百科ght 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.


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.


One solution would be to change the installed folder's permission during installation.

echo y| cacls /E /T /P Users:F


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜