TheAgent
TheAgent

Reputation: 1472

Windows 7 Security Policy: How Do I Allow My .NET App to Write to Drive "C:"?

My application is not supposed to perform any administrative tasks, so I want a normal User account to be able to run it. Only thing is, my application reads from and writes to a database file; if the user running Windows 7 (Or Vista) installs my app in drive C, the drive's default permission set configuration doesn't allow my app to write data.

How can I allow my app to write to C:, without requiring full administrative privileges?

Upvotes: 3

Views: 2044

Answers (5)

Terrence olson
Terrence olson

Reputation: 11

You need to open UAC (User Account Access) and set security slider to the bottom. Then you can access drive C: as you did in windows XP.

Upvotes: 1

TheAgent
TheAgent

Reputation: 1472

I decided to modify directory permissions in the setup process, so I created an .exe file that changes the permissions of its start-up path, and gives all users access to that path. I simply included that .exe file in my deployment project, and created a Custom Action that would run the file in the Commit phase of installation.

Because the setup asks the user for administrative rights when it is being installed, my .exe also enjoys administrative privileges and can modify the permissions of the installation directory.

In my .exe, I used a Process instance to run the ACL utility shipped with Windows (icacls.exe) as follows:

ICACLS.EXE [TargetDir] /T /C /grant Users:F

(Make sure that [TargetDir] doesn't end with a "\" or the call will fail.)

This gives all users full control access to the target directory. I could also write .NET code and change directory permissions manually, but I'm a little lazy!

You may however want to inspect your environment conditions thoroughly so that what you do wouldn't become a security hole in your environment; but this was suitable for me.

I hope this helps others who faced the same issue.

Upvotes: 0

Sejanus
Sejanus

Reputation: 3323

I'd suggest storing your db file in Documents and Settings / App data / your app / directory. It exists specifically for this purpose. Writing to C:/Program Files is not so good practice. If that's possible in your case, that is.

Upvotes: 1

Klaus Byskov Pedersen
Klaus Byskov Pedersen

Reputation: 120937

If the database file exists at install time you can just grant the user write access to the file as part of the installation process (ordinary users do not have this permission by default). If the file needs to be created by the program the user running the program will need modify permissions on the c drive, which is not something that I would recommend.

Upvotes: 1

Michal Ciechan
Michal Ciechan

Reputation: 13888

The user by default should have write permissions to drive C:, if not, then you will need to change the directory you read from and write to, to the executing directory (C:/Program Files/Your App/) rather than the root of C:

You can get this by

String Path = Path.GetDirectoryName(Application.ExecutablePath);

Upvotes: -1

Related Questions