Solved

How to create a C# configuration file

Posted on 2010-09-07
3
326 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

734 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