Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Application can't read/write to files created by administrator when run in limited user account XP

I have an application that is useable by all users (admin or limited) in .NET (C# specifically).

When the application first launches - it creates a few files that it needs in the C:\Documents and Settings\All Users\Documents\ for all subsequent launches.

If the limited user in XP is the FIRST user to launch the application it creates the files fine and both the limited user and administrators can run fine.

However if the Administrator (or I am guessing a different limited user) is the first to launch the application then the limited user is NOT able to run the application.

The two files that it is NOT able to read/write to if created by an Administrator is a Log4Net log file and a SQLite db file.

The SQLite database file is being created with a straitforward .NET File.Copy(sourcepath, destinationpath). The sourcepath is a seed database file installed with the application - so on first run it copies that from the C:\Program Files\app install\seed.db

Is there a way to set the permissions on the file when I copy it? File.SetAccessControl() perhaps? I am not clear on how that works.

The other issue is that the log4Net rolling file appender will not roll the old file and create a new as the old file was created by the admin user when they ran the app.

Any ideas? Ironically this all works perfectly fine in Vista with limited/admin accounts - this is ONLY happening in XP with admin/limited accounts.

like image 609
jonathanq Avatar asked Feb 11 '09 00:02

jonathanq


2 Answers

I think SetAccessControl is the way to go. Maybe something like this:

// get the existing access controls
FileSecurity fs = File.GetAccessControl(yourFilename);

// add the new rule to the existing settings
fs.AddAccessRule(new FileSystemAccessRule(
    @"DOMAIN\Users",  // or "BUILTIN\Users", "COMPUTER\AccountName" etc
    FileSystemRights.Modify,
    AccessControlType.Allow));

// set the updated access controls
File.SetAccessControl(yourFilename, fs);

Note: It's important that you get the existing access control list from the file and then add your new rule to that. If you just create a new access control list from scratch then it will overwrite the existing permissions completely.

like image 178
LukeH Avatar answered Nov 14 '22 23:11

LukeH


Yeah, it's the SetAccessControl method all right, there is a good example here (the post from satankidneypie)

Good luck

like image 35
Michael Walts Avatar answered Nov 14 '22 22:11

Michael Walts