I would suggest you study the File.SetAccessControl()
method and FileSecurity
class. In particular, the FileSecurity
documentation provides a complete example of modifying the file access control list (permissions set) for a given file. Here is a simplified version of that example, which gives everyone read and write permission for C:\TEMP\junk.txt
:
using System;
using System.IO;
using System.Security.AccessControl;
using System.Security.Principal;
class WorldWrite
{
static void Main()
{
string filename = @"C:\TEMP\junk.txt";
if (!File.Exists(filename)) {
Console.WriteLine("No such file " + filename);
return;
}
/* Get the previous ACL, add a rule to give everyone read
* and write permission, and write back the resulting ACL. */
FileSecurity fileACLs = File.GetAccessControl(filename);
fileACLs.AddAccessRule(new FileSystemAccessRule(
// For everyone
new SecurityIdentifier (
WellKnownSidType.WorldSid,
null /* domain SID; ignored for Everyone */
),
// Allow read and write (of data and metadata),
// but not delete or execute
FileSystemRights.Read | FileSystemRights.Write,
AccessControlType.Allow
));
File.SetAccessControl(filename, fileACLs);
Console.WriteLine("Success!");
}
}
I'm also concerned about how you are specifying the shared application data directory. If you're hard-coding the name C:\Documents and Settings\All Users\Application Data
, then your code won't work on non-English versions of Windows, or Vista or Windows 7 for that matter. It may be better to use the SpecialFolder
enum to find the right directory to use.