where do I find a config file parser (xml or ini file)?

  • Thread starter Thread starter Egil Hansen
  • Start date Start date
E

Egil Hansen

Hi

I need to pass 5-6 simple settings to a console program I'm writing. I
thought about putting them in a simple .ini file or a .xml file. I'm
also thinking about simply passing them to my program as commanline
arguments, that would be a rather long command to type in though, but
it would work.

Anyway, being a newbie at C#, how do I do either best?
Best regards,
Egil.
 
It is easy to have your app settings placed in an XML file and then use it
in your application. I will show you an example:

First you need the config file. In my case it is appname.exe.config. You
will have to add a application configuration file to your project in order
to get it. Just call the file anything you like. I used app.config.
<?xml version="1.0" encoding="utf-8"?>

<configuration>

<appSettings>

<add key="frmMain.Text" value="Application name ver 2" />

<add key="frmConfig.Text" value="Configuration" />

</appSettings>

</configuration>

In the XML file above you see that i have 2 forms. The text that will appear
in the top of each form is placed in the config file. Now lets have a look
at the code that is using this "setting".
AppSettingsReader configurationAppSettings =new AppSettingsReader();

appName = ((System.String)(configurationAppSettings.GetValue("frmMain.Text",
typeof(System.String))));

Thats it. As you can see i set an variable appName = the setting from the
XML file. This can easily be passed on to your form.text or anything you
want. Remember to include "using System.Configuration"

Best regards
Trond
 
Hi again Trond.

I finally got around to testing your idea, but I cant get it to work. I
even tried using the same key's and values as you, but still gets an
expection telling me that the key i'm looking for doesnt exists. Can
you help a little more.

Thanks, Egil.
 
Sure. If you open a form and then go to the forms property you will find
"Dynamic properties". Click on that lil + so that you can see a field called
(Advanced) (still in DynamicProperties). Click on the little button with 3
dots. Now you will se a dialogboks. Scroll a little don and select "Text".
In the textbox at the right part of the dialog you will see a text
"frmName.text". Clik OK and voila you will find that in the app.config file.
When you compile you will see that an additional config file appear. That
will have the name appname.exe.config.
Hope this help. If not please drop me a mail and i'l try to help
Best regards
Trond
 
Back
Top