C# – How to Save a setting for All users under Vista

cnetvb.netwindows 7windows-vista

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

Best Answer

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.