Gigi Labs

Saturday, March 22, 2014

C# AppSettings: Launching Programs and Storing AppSettings

Hi all! :)

Today we're going to write a little program that can start other programs. For example, it might launch MS Word, or your favourite browser. From this we will learn how to actually start other programs using C#, and also how to add settings into an application configuration file so that they can easily be changed.

Start off by creating a new Console Application using your favourite IDE. Throw out any code already in Main(), and add this initial code:

Console.Title = "Launcher";

// TODO code goes here

Console.ReadLine();

Now, to start a program from our code, we need to use the Process class. To be able to use it, we need to add the following to our usings:

using System.Diagnostics;

Starting a program is really easy. All we need to do is replace our "TODO" comment with the following:

And sure enough, when we run our program, Notepad++ is opened as well:

Let us now take this a step further. We can allow the user to type in the name of an application, and the corresponding application will be launched. We could do this using a switch statement as in "C# Basics: Command Interpreter Using Methods". Or even better, we can use a Dictionary to store the relationship between application names and their path. First, add the following using at the top if it's not there already (e.g. if you're using SharpDevelop):

The user gives us the name of the program he wants to launch. If that name exists in our dictionary, we get the corresponding path, and then launch it as we did before. If we try this now, it works pretty nicely:

Although this works pretty well, it isn't very flexible to have the program names hardcoded in your program code. Imagine you give this program to a friend. It wouldn't be very useful if he could only launch Firefox and Notepad++. What if he wanted to be able to launch SharpDevelop as well? And maybe in future he might want to add VLC as well. It obviously isn't convenient for you to have to change the code, recompile, and give him an updated program each time.

One option could be to store these program settings in a file and read it, but .NET provides us with a simpler option that exists specifically for this kind of scenario. .NET applications can optionally come with an application configuration file, which is usually called App.config. In fact, if, like me, you're using SharpDevelop 5 Beta 2, you should have one already (otherwise just add an Application Configuration File to your project):

When you build the application, App.config gets renamed to the name of the executable, plus a ".config", and is stored in the same folder:

In this App.config file, under the <configuration> node, we can add an <appSettings> node and store our settings. The settings have a key, which is a name that identifies them, and a value which is the value of the setting. Ours will be like this:

With this done, we can now rewrite our program to use these settings. First, we need to add a reference to System.Configuration. To do this, right click on the name of the project in Solution Explorer, and select "Add Reference":

In the "Add Reference" window, locate "System.Configuration" and then click "OK".

Next, add the following using statement at the top:

using System.Configuration;

We can now use ConfigurationManager to obtain our app settings, like this:

The only thing worth noting here is that if the specified key does not exist in the AppSettings, retrieving that key via ConfigurationManager.AppSettings[] returns null, allowing us to take appropriate action in that case.

So now, when you give this program to a friend, he can just add the following line in the CsAppSettings.exe.config file that conveniently comes with the program:

<add key="fiddler"value="C:\tools\fiddler2\Fiddler.exe" />

...and without further ado, he can now launch Fiddler as well:

Wonderful! :)

In this article, we learned how to start another program from our code, and we also learned how to work with application settings in an application configuration file.

Storing your application settings in an App.config file is convenient because they can be tweaked as needed, without having to recompile your program. These kinds of settings are best suited for settings that don't change frequently, so you just set them when you need them, and forget about them. In the next article, we'll learn about another kind of settings that are great for storing stuff like user preferences which can potentially change a lot.