Johann
Johann

Reputation: 29867

Sharing data between apps under Windows

I would like to share data between apps. My apps run using Microsoft .Net. I don't need the data to be shared in the sense that one app passes data to another through an API but rather one app stores the data somewhere and the other apps can access it when it needs it. The most obvious choice is to just use a file and store it to disk in a folder that all apps can find, either because the path is hardcoded in the apps or the path is stored in something like the system registry. This isn't the best solution though since hardcoding paths is ugly.

What options do I have? Is there a .NET API that lets me store stuff in some common location?

Upvotes: 1

Views: 310

Answers (1)

O. R. Mapper
O. R. Mapper

Reputation: 20732

While hard-coding an absolute file path can indeed be considered ugly (your application could run on a system where that path simply cannot be created), the way to go is retrieving a general location for storing application-specific data from the environment and then hard-coding a relative path below that. This is commonly done for settings files, and it generally works, relying on the thought that if you name your relative path by something like your company and your product name, collisions with anything else on the same system are already sufficiently unlikely.

The Environment.GetFolderPath method can be used to retrieve a path where to store configuration data, for example SpecialFolder.ApplicationData.

Use the Path.Combine method to append a name that is hard-coded and the same in all of your applications that require the common data. Presumeably, as they are supposed to work together, each of those separate applications belongs to a common project or framework, so you can name the directory after that:

string configPath = Path.Combine(Environment.GetFolderPath(SpecialFolder.Application.Data), "AndroidDevsComplexToolkit");

(This example uses C#. The same methods can of course be invoked from other .NET languages, as well.)

EDIT: This answer elaborates on the file-based approach. Depending on the type of the data (if there are large amounts of structured data), requiring for the system to have an installation of some database might be a more appropriate way. In that case, the "common location" as stated in each of your separate programs would be the connection data to the local database server.

Upvotes: 1

Related Questions