views:

288

answers:

2

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

+5  A: 

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.

Quintin Robinson
Awsome! Exactly what I was looking for. Thanks!
Ben Breen
+3  A: 

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)
    
mdb
Thanks, this would have certainly caught me out!
Ben Breen