15

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.

superjos
  • 12,189
  • 6
  • 89
  • 134
paulinodjm
  • 205
  • 1
  • 2
  • 6
  • Have your tried?Windows.Storage.ApplicationData.Current.LocalFolder; – Xeun Jan 13 '15 at 09:36
  • Is the Windows namespace also available on WindowsForm - .Net 4.0? – paulinodjm Jan 13 '15 at 09:40
  • Hmm, good question, but I don't think so, sorry. But you should definetly find a path which matches your desires here: http://www.codeproject.com/Tips/370232/Where-should-I-store-my-data – Xeun Jan 13 '15 at 09:44
  • 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. – Xeun Jan 13 '15 at 09:48
  • 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) – paulinodjm Jan 13 '15 at 09:51
  • 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! – Xeun Jan 13 '15 at 09:55
  • 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 :$ – paulinodjm Jan 13 '15 at 10:00
  • 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! – Xeun Jan 13 '15 at 10:03
  • 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\version – Hans Passant Jan 13 '15 at 10:29
  • @HansPassant Are you sure about that? [On my machine](http://i.stack.imgur.com/pSsdx.png) `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. – Scott Chamberlain Jul 22 '15 at 18:38
  • 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. – Hans Passant Jul 22 '15 at 18:43

2 Answers2

14

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.

Akku
  • 4,373
  • 4
  • 48
  • 67
  • 1
    For the newbs like me, GetFolderPath and SpecialFolder are in Environment, and Path is in System.IO – geriwald Oct 13 '20 at 09:12
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.

superjos
  • 12,189
  • 6
  • 89
  • 134