Solved

How to create a C# configuration file

Posted on 2010-09-07
3
319 Views
Last Modified: 2012-05-10
Hello,

Need to create a config file (text file) that contains metrix associated with parameters. Application design doesn't store info need a quick workaround.

Line 1- Height Metres
Line 3- Cost AUD($)
etc

Height, Weight are used for charting.  Comboboxes populated with these strings and values are double arrays.  

Should I parse the text file and insert into a Dictionary<key, value>.

Key equals the name of the parameter and Value is the metrics?

Better approach to achieve this?

Thanks AC

0
Comment
Question by:AndyC1000
  • 3
3 Comments
 
LVL 16

Expert Comment

by:kris_per
ID: 33622789

One possible quickest method is: Using XmlSerializer will save lot of coding in mapping values between variables and files as shown in the code below.
public class ConfigItem

        {

            public string Name;

            public double Value;

        }



List<ConfigItem> list = new List<ConfigItem>(); // Dictionary can not be used with xmlserializer

            list.Add(new ConfigItem() { Name = "Height ", Value = 1.5 });

            list.Add(new ConfigItem() { Name = "Cost ", Value = 50.25 });



            // To save the config items to a xml file

            XmlSerializer x = new XmlSerializer(typeof(List<ConfigItem>));

            TextWriter writer = new StreamWriter("myconfig.xml");

            x.Serialize(writer, list);

            writer.Close();



            // to load config items from xml file

            XmlSerializer xs = new XmlSerializer(typeof(List<ConfigItem>));

            TextReader r = new StreamReader("myconfig.xml");

            List<ConfigItem> clist = (List<ConfigItem>)xs.Deserialize(r);

            r.Close();

Open in new window

0
 
LVL 16

Expert Comment

by:kris_per
ID: 33623187
Another quick method:
Dictionary<string, double> d = new Dictionary<string, double>();

            d["Height"] = 1.5;

            d["Cost"] = 25.50;



            // To save config values in a config txt file

            TextWriter writer = new StreamWriter("myconfig.txt");

            foreach (KeyValuePair<string, double> item in d)

            {

                writer.WriteLine(item.Key + "=" + item.Value);

            }

            writer.Close();



            // To read from config txt file

            Dictionary<string, double> d2 = new Dictionary<string, double>();

            TextReader r = new StreamReader("myconfig.txt");

            while (true)

            {

                string line = r.ReadLine();

                string[] t = line.Split('=');

                d2[t[0]] = Convert.ToDouble(t[1]);

            }

            r.Close();

Open in new window

0
 
LVL 16

Accepted Solution

by:
kris_per earned 250 total points
ID: 33623201
With a correction in the above code:
...

// To read from config txt file

            Dictionary<string, double> d2 = new Dictionary<string, double>();

            TextReader r = new StreamReader("myconfig.txt");

            while (true)

            {

                string line = r.ReadLine();

                if(line == null) break;

                string[] t = line.Split('=');

                d2[t[0]] = Convert.ToDouble(t[1]);

            }

            r.Close();

Open in new window

0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now