Author Topic: Loading INI files via C#  (Read 3636 times)

0 Members and 1 Guest are viewing this topic.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4076
Loading INI files via C#
« on: August 12, 2010, 11:35:15 AM »
Having searched theSwamp and Google for best practices, I found a lot of pages that implied that C# does not play nice with INI files.  My question is what is the best way to load information into a dialog box combobox dropdown?  In VBA, I used ini files and just read each line and added it to the combo box.  After testing, I can do the same via C#, but could not find a best practice example anywhere.  Should I be looking at something else or stick with the old method?
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

mohnston

  • Bull Frog
  • Posts: 305
  • CAD Programmer
Re: Loading INI files via C#
« Reply #1 on: August 12, 2010, 11:55:42 AM »
For simple stuff I use the Settings that Visual Studio handles. (config file)
For lists of things I use xml.
The xml file can be a simple table or a dataset that contains many tables.
But DataTable and DataSet both have ReadXML and WriteXML methods that are easy to use (remember to save the schema with them)
It's amazing what you can do when you don't know what you can't do.
CAD Programming Solutions

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4076
Re: Loading INI files via C#
« Reply #2 on: August 12, 2010, 11:57:38 AM »
Thanks Mark!  I was thinking of using XML, but wanted to hear what you guys had to say first.
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: Loading INI files via C#
« Reply #3 on: August 12, 2010, 12:34:54 PM »
Seconded on XML.  I find it easier to manage in the long run and with a little XSL, the contents can be transformed to HTML to make them easier to review.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4076
Re: Loading INI files via C#
« Reply #4 on: August 12, 2010, 12:39:50 PM »
As a side note to this same discussion, Glenn was helping me with some other code, and made the suggestion to not use Switch statements on text, but rather on an integer that could be used for an ennumeration.  For weeks I could not see where I would use this, but I think this is a great place.  The above mentioned drop down can/will have 50 plus text values to choose from, so switching on integer will be faster.  Ideas?
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4076
Re: Loading INI files via C#
« Reply #5 on: August 12, 2010, 01:10:36 PM »
I guess I should add that I am asking if the XML that is read from, should be loaded into an ennumeration, or just switch on the text value?
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

mohnston

  • Bull Frog
  • Posts: 305
  • CAD Programmer
Re: Loading INI files via C#
« Reply #6 on: August 12, 2010, 01:56:42 PM »
As a side note to this same discussion, Glenn was helping me with some other code, and made the suggestion to not use Switch statements on text, but rather on an integer that could be used for an ennumeration.  For weeks I could not see where I would use this, but I think this is a great place.  The above mentioned drop down can/will have 50 plus text values to choose from, so switching on integer will be faster.  Ideas?
I don't think speed will be an issue unless you get into thousands of choices with many columns.
By the way there are many simple XML file viewers available. For example this free one at this cool website. http://www.cadprogramming.net/products.htm
It's amazing what you can do when you don't know what you can't do.
CAD Programming Solutions

LE3

  • Guest
Re: Loading INI files via C#
« Reply #7 on: August 12, 2010, 02:16:42 PM »
apart of Mark cool web site :), have a look too at codeproject.com, in there are plenty of samples about read/write xml classes written in C# - HTH.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4076
Re: Loading INI files via C#
« Reply #8 on: August 12, 2010, 02:34:51 PM »
I dont have thousands thankfully.  I was more interested in the best practice.  I do know that when you Switch on Text, it has to match EXACTLY.  If you cap some thing in one, and not in the other, your hosed.
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4076
Re: Loading INI files via C#
« Reply #9 on: August 12, 2010, 02:35:57 PM »
Thanks LE, I like that site for the great examples.

Thanks Mark for the viewer, its easier than viewing in VS
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

mohnston

  • Bull Frog
  • Posts: 305
  • CAD Programmer
Re: Loading INI files via C#
« Reply #10 on: August 12, 2010, 03:04:25 PM »
I dont have thousands thankfully.  I was more interested in the best practice.  I do know that when you Switch on Text, it has to match EXACTLY.  If you cap some thing in one, and not in the other, your hosed.
I usually myString.Trim().ToUpper() both values before I do any matching for just that reason.
It's amazing what you can do when you don't know what you can't do.
CAD Programming Solutions

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4076
Re: Loading INI files via C#
« Reply #11 on: August 12, 2010, 03:11:43 PM »
A question about the number of XML files.  Is it better to have 2 separate files, or internal pieces in the XML that diferenciates what value i.e.
Code: [Select]
<?xml version="1.0" encoding="utf-8"?>
<ValidChoices>
  <Choice>
    <LineNumber>1</LineNumberk>
    <TextString>"SomeValueForLine1"</TextString>
  </Choice>
  <Choice>
    <LineNumber>3</LineNumberk>
    <TextString>"SomeValueForLine3"</TextString>
  </Choice>
</ValidChoices>
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)