I am trying to generate a license key file in the common application data location in windows so when a standard user account tries to access my application, the application can check to see if the license key is valid. The license file is being created using an admin account during install but when a standard user account tries to access the file, a System.UnauthroizedAccess exception is thrown.
Here is the code I use to create the directory that I store the license key file.
FileSystemAccessRule fsAccessRules = new FileSystemAccessRule("USERS", FileSystemRights.FullControl, AccessControlType.Allow);
Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\COMPANYNAME\\");
Directory.GetAccessControl(Environment.GetFolderPath(Environment.SpecialFolder.CommonAppli开发者_Go百科cationData) + "\\COMPANYNAME\\").AddAccessRule(fsAccessRules);
Thanks.
I believe the problem is that you're only modifying the FileSystemSecurity
object in memory, meaning you're not saving the changes. From the docs for AddAccessRule
, it looks like you need to call SetAccessControl
to save the changes back to disk after calling AddAccessRule
. This will update the security descriptor on the file with the access rule you added.
This CodeProject article has some good sample code: Allow write/modify access to CommonApplicationData
One other thing to consider: if you only need users to be able to read this file, you might consider granting them a more limited set of rights, such as FileSystemRights.Read
. This would prevent the users from actually modifying the license file, assuming they don't need that ability.
精彩评论