开发者

How to Save a setting for All users under Vista

开发者 https://www.devze.com 2022-12-08 00:57 出处:网络
I need to save a setting which is then available to all users of the application on the given machine. It needs to work on Vista/Win 7, and the application won’t be started as administrator.

I need to save a setting which is then available to all users of the application on the given machine. It needs to work on Vista/Win 7, and the application won’t be started as administrator.

  • Can’t Save to program directory as the Program Files folder is protected on Vista
  • Can’t save to HKEY_LOCAL_MACHINE as that’s protected too
  • Can’t save to server or web-service

Where can I save data to? Even if the application rights were somehow elevated during execution, my worry is that开发者_开发百科 the registry is now virtualised in Vista – and so I will end up with a special HKEY_LOCAL_MACHINE which is actually only for the current user.

I'm using .NET


Common Application Data

System.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)

This is a filesystem path that you can use to save data across multiple users and multiple versions of the operating system. The path might be different depending on the version but because you are using environment variables the above line will resolve to a usable path.

Edit:
Wanted to add this as a note since it was only implied; it does not require elevated permissions to write to this directory, it is meant for exactly this purpose.


As Quintin correctly replied, the %ALLUSERSPROFILE% path (Environment.SpecialFolder.CommonApplicationData in .NET) is what you're looking for.

Two important things to keep in mind, though, when doing so:

  • It's good practice to create a subfolder for your company and application using your application installer. E.g:

    Dim DataPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
    DataPath = IO.Path.Combine(DataPath, "ACME Corp.")
    DataPath = IO.Path.Combine(DataPath, "Widget App")
    DataPath = IO.Path.Combine(DataPath, "1.0") '//Optional, but possibly handy to easily migrate configuration files across major app versions
    
  • Although all users will have read access to the folder you create, write access is by default limited to the the account that created the folder, as well as members of the Administrators group and LocalSystem. If all users should be able to modify a common configuration file (created by one user, but re-written by another non-Admin user), you'll need to arrange for this access explicitly. Again, this is best done from your app installer, but could be done from code as well, for example when your app is first run:

    Dim di As New IO.DirectoryInfo(DataPath)
    Dim ds = di.GetAccessControl
    ds.AddAccessRule(New Security.AccessControl.FileSystemAccessRule(...))
    di.SetAccessControl(ds)
    


Here is a workaround that lets you persist settings for all users:

  1. Put a settings file in program files to be read by your application.
  2. To modify this settings file, create a new application designed to write to the settings file.
  3. Launch this settings modifying app from your main app with the verb set to 'run as'

    Process p = new Process();
    p.StartInfo = new ProcessStartInfo("changeXMLSettings.exe");
    p.StartInfo.Verb = "runas";
    p.Start();
    

This will prompt for elevation and give the settings modifying app permission to write to the program files directory.

0

精彩评论

暂无评论...
验证码 换一张
取 消