Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining Write Permissions to the Application Folder

I have a C# application, and I need to dump some output to a log file during operation. I am wanting to give the user the option of where to locate the log file, but by the client request it needs to default to the current application location, which is normally /Program Files/.

When I deploy my application on a Win7/Vista machine, though, the application does not write the log file unless I run the program as an Administrator. At the same time, it seems to be silently handling the case where it cannot write the file, as I am currently handling all exceptions being thrown during the file creation and writing process.

I am currently trying to detect lack of write permission by both:

A) Creating a DirectorySecurity object by calling "Directory.GetAccessControl()" and

B) Checking security priviledges with the "SecurityManager.IsGranted(permissions)" method,

but A does not throw an exception when I expect it to, and B returns true every time.

I have seen numerous posts related to this topic, but they all give the solution of just writing to Application.UserAppDataFolder or some variation of it. My client has specifically asked to default to the current Application path, so I need to at least find a way to gracefully warn them when writing the log file is going to silently fail.

Note: My current code works find on Windows XP (since there are no UAC, I assume). Basically all I need to know is why all my calls are telling me that writing the file is going fine, when the file is never created at all unless I am running as Admin.

Thanks!

like image 600
njv299 Avatar asked Nov 12 '10 16:11

njv299


2 Answers

Windows Vista and 7 will write files to the Program Files directory just fine.

Well, not really, but the program thinks it's just fine. In reality, the file is written to the current user's VirtualStore directory; that is, in %userprofile%\AppData\Local\VirtualStore\Program Files

You can include a manifest file to disable this behavior for your application to get the results you expect.

like image 95
Powerlord Avatar answered Oct 10 '22 20:10

Powerlord


You can force the os to run your app as Admin.

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
like image 23
Stefan P. Avatar answered Oct 10 '22 19:10

Stefan P.