How to save the setting for all users under Vista

I need to save a parameter, which is then available to all users of the application on this computer. It should work with Vista / Win 7, and the application does not start as an administrator.

  • Cant Save to the program directory as the Program Files folder protected on Vista li>
  • Cannot save in HKEY_LOCAL_MACHINE, like thats protected too
  • Unable to save server or web service

Where can I save data? Even if the application’s rights were somehow elevated at runtime, I’m worried that the registry is now virtualized in Vista - and therefore I get a special HKEY_LOCAL_MACHINE, which is actually only for the current user.

I am using .NET

+5
source share
3 answers

General application data

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

This is the file system path that you can use to save data for multiple users and multiple versions of the operating system. The path may vary depending on the version, but since you are using environment variables, the above line will resolve to the path used.

Edit:
I wanted to add this as a note, as it was only implied; it does not require elevated permissions to write to this directory, it is intended for this purpose.

+9
source

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

It is important to keep in mind two important things:

  • . :

    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
    
  • , , , "" " ". ( , , ), . , , , , :

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

, :

  • Place the settings file in the program files that will be read by your application.
  • To modify this settings file, create a new application designed to write to the settings file.
  • Run this setting by changing the application from the main application, with the verb set to "work as"

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

This will cause elevation and make it possible to change the application settings for writing to the program file directory.

-1
source

All Articles