user3297129
user3297129

Reputation: 299

Suggest a method for config file

I have a c# application where I have methods say, method1 , method2 .... method n. Each of these methods are features. I dont want my application to execute all the features all the time but, it should be configurable. So I am in need of a config file where what features to execute. Please suggest me a method to do this. I am thinking of using an XML file to store the method names and using reflection to invoke them. But, is there any PROFESSIONAL way to do this ?

Upvotes: 0

Views: 207

Answers (2)

TaW
TaW

Reputation: 54433

Your question consists of several subqusetions. Working professionally imo begins with breaking down a problem into its most elmental parts.

Let's see:

  • Where should the data go? Either text files of the Registry come to mind.
  • What names should it get? Readable and meaningful, like YourApplication.config.
  • Who creates and stores the data?

You didn't tell us; could be the same application or a diferent one or a human being. In the latter case make sure the data are not misspelled. The usual way is to list all valid names and have the user simply add or remove a comment mark at the start of a line.

  • How should they be stored? Doesn't matter unless: see above!
  • And finally how should they be used? Using reflection seems overkill, unless you have a system where you don't know all possible functions in advance because new function can be plugged-in. Otherwise simply use a if else if block or for something more fancy create a Dictionary<string, Action> or the like..

Upvotes: 0

Stuart Grassie
Stuart Grassie

Reputation: 3073

C# applications have an app.config (for desktop apps) or web.config (for asp.net apps) file. In this file you can specify settings, like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
  <appSettings>
    <add key="something" value="1"/>
  </appSettings>
</configuration>

In your application code, you can access these settings like this:

var something = ConfigurationManager.AppSettings["something"];

Note: You'll have to add a reference to System.Configuration to access the ConfigurationManager.

When you compile your application, the app.config gets transformed into YourApp.exe.config, and you can deploy it alongside your application to control the settings without having to recompile your application.

You can read more about the configuration manager, and the app.settings on MSDN.

Upvotes: 1

Related Questions