Where to store configuration for a simple .NET app

2019-01-23 17:44发布

问题:

I'm programming a fairly simple application which I want to cut to just one simple EXE file + some data storage (XML for example).

My question is regarding configuration files. Where to put those files? I saw a few applications that have just an EXE file (uTorrent, Media Player Classic - I can use them without any installation), but they store their config somewhere else. How to achieve this?

How would you approach such situation? Is it better to try to achieve the thing I described above, or simply use a configuration file and data storage in the same directory as the EXE file?

回答1:

I think you want to take a look at Application Settings. This is an API which allows you to save user or application settings using a strongly typed API. Under the hood the settings are stored via XML serialization.

This API works with virtually every type of .Net application including low permission Click Once versions. It does the work of finding the place on disk appropriate for storing the data and completely hides it from you. It also has a nice GUI integration into Visual Studio.



回答2:

Creating or using a file in the same folder (or in the App_Data) is pretty standard practice.

You use an installer like Inno Setup (free) to create a single exe installer (http://www.jrsoftware.org/isinfo.php)

If you want a DB rather than XML, have a look at SQLite (http://www.sqlite.org/) a file based DB or use an MS Access DB.



回答3:

EXE-only programs store their data either in the Windows Registry or in the user's Application Data/AppData folder. Although this may appear cleaner at first, it just hides the ugliness of scattering all your data around. I would suggest just going with a simple XML/INI/text data file that is generated when needed and easy to migrate.



回答4:

Please see: WPF/C#: Where should I be saving user preferences files?



回答5:

You could use the app.config file for storing your configuration. For the data, I would sugest something like db4o or SQLite.

Edit

This tutorial can show you how simple is to use db40 to store and retrieve your data.



回答6:

Do not forget Isolated Storage. It gives you a place to read and write files to without the need for you to specify a location. Sometimes it is the only way sandboxed applications (like Silverlight) can store user or machine specific data locally. See here for an example.



回答7:

I would store them in the same directory. That just seems easier to me, at least that's the way I always do it.