简体   繁体   中英

How to go about creating a configuration file for my application in .NET

I am working on a software and want to create a configuration file for my application to store configurations that will be used at runtime. I have seen some software use a config.xml file to achieve this. The configuration file I want to use will have:

  1. Cache folder location
  2. Color scheme
  3. Option to toggle caching

How to go about this? I am working with WPF.

In Visual Studio, add an "app.config" to your project. When you build your application, this will create an AppName.exe.config file, where AppName is the name of your executable. This is an XML config file that can contain your settings.

因为您在WPF世界中,您还可以使用App.XAML来定义和使用颜色方案

If theses options are user-specific, don't use app.config - that's for global application settings and you will clobber other users' settings. I'm bringing this point up because you mentioned caching options in your question, and in some applications, the choice to cache or not to cache is at the user level.

Instead, I would recommend that you create a domain object to store the configuration settings to, and serialize the object to a local folder in the current user's isolated storage folder.

When the application starts up, just look into the current user's isolated storage folder, confirm the serialized file exists, and deserialize back to the domain object.

Use standard .NET settings framework.

More details here: How is the logic behind storing program setting in text file like 'config.cfg'?

Also this will help http://msdn.microsoft.com/en-us/library/aa730869%28v=vs.80%29.aspx .
Although this article is a little old, not much changed since then in this realm.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM