Writable folder by all users on the same pc
I have a desktop .NET WPF application witch uses an embedded database (SQLite). Where to put the database file ? It's the same database for all users.
I tried to use CommonAppData but it's not writable by non-admin users. So I tried to use a custom installer action to give write rights to all users to this folder but it fails on domain PCs. The code is:
string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "MyAppName");
DirectorySecurity security = Directory.GetAccessControl(appDataPath);
FileSystemAccessRule rule = new FileSystemAccessRule("Users", FileSystemRights.WriteData, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInheri开发者_JAVA百科t, PropagationFlags.InheritOnly, AccessControlType.Allow);
security.AddAccessRule(rule);
Directory.SetAccessControl(appDataPath, security);
Would ".\Users" insead of "Users" work on a domain ?
Is this the best approach ? Is there any other folder I could use ?
There is no folder you can use by default. You need to adjust the ACL of a directory otherwise the default right is only going to include read access for Users other than the files Owner (creator).
Typically you do have all rights to directories you create yourself - so my approach was to create a application sub folder in the ProgramData folder, then, as the owner (via creation)of the new object, I had full control over that and could add my all-users-full-access as an inheritable access control entry to that.
I also ran into problems with "Users" using the Windows API and had to switch to using a well known sid. Tested on a German system at least the "Users" string was entirely different.
精彩评论