问题
In Windows other than the registry -localmachine where can I place common user data that could be accessed by all users.
i need to do this without requesting elevation of admin rights
回答1:
You can store it in the Application data folder. which you can get from Envorinment:
var appDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments);
To determine if you can access the folder without admin right, run Visual Studio, without admin rights, and see if this code executes successfully.
class Program
{
static void Main(string[] args)
{
var folder = Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments);
var file = Path.Combine(folder, "testfile.txt");
File.WriteAllText(file, " Test Settings");
Console.ReadLine();
}
}
回答2:
No such location is available by default. Regular (i.e., non-administrative users) do not have the requisite privileges to mess with system files or files that belong to other users—that would be a security issue. As such, they would not be able to write to a "common" directory.
If you need all users to be able to write to some type of common area, you need to set it up yourself. Generally, this is done by an installer application. The installer application will need to request elevation so that it has administrative privileges in order to write to restricted folders and alter security rights. If you're using a standard installer utility, then this is usually a built-in feature. If you're writing your own, you arrange for it to have administrative privileges by embedding a manifest with level
set to requireElevation
(search Stack Overflow for details).
The installer it will create a sub-directory of the common application data folder (the enumerated value Environment.SpecialFolder.CommonApplicationData
in the .NET world) for your application's use. By default, of course, that sub-directory will inherit the restrictions of its parent folder, so the installer will also need to explicitly set the ACL (access control list) on that sub-directory to grant all users write access. This can be done in the .NET Framework using the Directory.SetAccessControl method.
Then, after installation is complete, administrative privileges will not be required to run your application. The application can just save into its own sub-directory of the common application data folder, which its installer has ensured that all users have read/write access to.
来源:https://stackoverflow.com/questions/34922712/where-can-i-place-common-user-data-other-than-localmachine-registry-without-adm