3

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

techno
  • 6,100
  • 16
  • 86
  • 192
  • http://stackoverflow.com/questions/4514929/save-application-preferences-registry-file – spender Jan 21 '16 at 11:55
  • So long as the data is not _too large_, you may want to consider _Isolated Storage_ –  Jan 21 '16 at 12:09
  • @Micky its just some licensing data.I think there is no way to write to `local machine` registry other than creating a new program with admin rights.My app does not require admin rights now. – techno Jan 21 '16 at 12:21
  • That's the nice thing about Isolated Storage, it gives you a unified and abstracted way to write to _user; machine; assembly; app_ or combination there-off so that a user or groups of users can read/write data without running into security hickups or really caring _where_ it will be stored. It just works :) –  Jan 21 '16 at 12:25
  • @Micky Does it work without Admin rights? – techno Jan 21 '16 at 13:01
  • @techno yes. It was designed for that, particularly with ClickOnce where apps are installed _without_ admin rights –  Jan 21 '16 at 13:05
  • @Micky Can you post some sample as the answer? – techno Jan 21 '16 at 13:44
  • @techno See my answer below' –  Jan 22 '16 at 01:05
  • @HarryJohnston Well no, _user_ can actually mean a "user" or machine. e.g. `IsolatedStorageFile.GetMachineStoreForApplication()` –  Jan 22 '16 at 01:13

2 Answers2

3

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();
        }
    }
jvanrhyn
  • 2,804
  • 19
  • 14
1

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.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • Well, the sort order of the answers changes, so I'm not sure which answer you're talking about. Jvanrhyn's answer is correct insofar as it goes. You will want to create a subdirectory of a common folder like `SpecialFolder.CommonDocuments`, but you can't write there without administrative privileges. So that's why you need to the installer dance. Or elevate on-demand the first time your application runs. If you're talking about Micky's answer, regarding isolated storage, I really have no idea how that works. – Cody Gray - on strike Jan 22 '16 at 06:46