How to get the application specific data folder (ProgramData)?
Asked Answered
S

2

18

I need to read and write files that contain application specific data, shared between all the users.

I tried to use Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), but it returns only C:\ProgramData.

My question is :

Does it exist a system like Path.GetDirectoryName(Application.UserAppDataPath), which will give me the exact folder to write, according to my application name and version?

Or is ProgramData not the right place to do that.

Thanks.

Schoof answered 13/1, 2015 at 9:33 Comment(11)
Have your tried?Windows.Storage.ApplicationData.Current.LocalFolder;Zeiler
Is the Windows namespace also available on WindowsForm - .Net 4.0?Schoof
Hmm, good question, but I don't think so, sorry. But you should definetly find a path which matches your desires here: codeproject.com/Tips/370232/Where-should-I-store-my-dataZeiler
Re-reading your question I might want to ask, why you not simply use the path to teh application itself? It is a directory which should be available to all users. You could also use the built in c# Settigns files and use a specila Applciation settigns file. This even makes saving loading and restorign of settings very easy.Zeiler
1) Isn't ProgramFile a read-only directory? 2) The most of the data are stored inside a sqlite file, not a app.config (if it is you are talking about)Schoof
Now I know what your are talking about :) Regarind a db file which is shared across multiple users I would use Environment.SpecialFolder.CommonApplicationData as you already mentioned. It resolves to: C:\programdata + adding a subfolder specific to your application in form of a guid and/or company name. I definately would use this approach!Zeiler
Okay, so if there is no build-in function to get the "rigth" directory (I'm afraid of collision name), I will use Path.Combine :$Schoof
Afraid of collision names? I don't see why there would be a collision, having the same directory twice is highely unusual, since the guid + company are most likely unique. But whatever fits your needs!Zeiler
c:\ProgramData does not permit write access, you'd need an installer that changes the user permissions to the subfolder. Not a good idea, use AppData instead. The "application specific folder" is just what you make it. Boilerplate is company\product\versionFay
@HansPassant Are you sure about that? On my machine C:\ProgramData has Write permission for the Users group to "This folder and subfolders" which means as a standard you should be able to create sub-folders in the Program Data folder.Myasthenia
Yes, quite sure. Lots of bad installers hack it, forced by programmers that test with UAC off and didn't realize that "programdata" does not mean "program data". I haven't been bitten by one yet, fingers crossed.Fay
C
19

I think CommonApplicationData is exactly what you're looking for, as it's the global folder for all applications which are not bound to a user.

var commonpath = GetFolderPath(SpecialFolder.CommonApplicationData);
var path = Path.Combine(commonpath, "YourAppName\\YourApp.exe");
try { 
    Process.Start(path);
    // or put data there or whatever
} 
catch (Exception ex)
{
    MessageBox.Show(path);
}

There's also SpecialFolder.LocalApplicationData for user-bound data.

Combination answered 9/7, 2018 at 6:54 Comment(1)
For the newbs like me, GetFolderPath and SpecialFolder are in Environment, and Path is in System.IOEternize
T
6

Does it exist a system like Path.GetDirectoryName(Application.UserAppDataPath), which will give me the exact folder to write, according to my application name and version?

No it doesn't exist, at least when running on Windows 7 (don't know about Windows 8/ WinRT/ Windows Store apps). Feasible solution is just to concat Environment.GetFolderPath(...) output with a custom path for your application. Typically, to reduce chances of clashing, that could be something like YourOrganization\YourApplication, or YourFullName\YourApplication, possibly also appending version.

Or is ProgramData not the right place to do that.

That is the right place to store application-wide information on disk. Information related to your application and different for each Windows user logging on the machine should go instead in <User folder>\AppData\Roaming\..., or <User folder>\AppData\Local\....

Beware: as somebody already mentioned in comments, normally one needs administrator rights in order to work inside C:\ProgramData..., hence you would need to prepare a setup project that, during install phase, would create the folder inside ProgramData and give the right permissions.

Tuberculin answered 22/7, 2015 at 18:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.