Solved

Read INI file

Posted on 2011-03-03
5
677 Views
Last Modified: 2012-05-11
Can someone tell me how to read an in file that has multiple values in each section from within a windows application  ?


e.g
[Servers]
ServerName 1=Server1
ServerName 2=Server2
ServerName 3=Server3
0
Comment
Question by:martin05
5 Comments
 
LVL 9

Expert Comment

by:rawinnlnx9
ID: 35030337
0
 

Author Comment

by:martin05
ID: 35030734
rawinnlnx9 thanks for the reply thats fine I can read the ini file for a single value, but how do you do it for multiple values ?


 INIFile ini = new INIFile("C:\\config.ini");

            for (int i = 0; i < ??????????; i++)
            {
                ini.Read ??("Server 1,???????????")
            }

0
 
LVL 29

Accepted Solution

by:
anarki_jimbel earned 250 total points
ID: 35033419
0
 
LVL 8

Assisted Solution

by:cubaman_24
cubaman_24 earned 250 total points
ID: 35035441
Hello:
In .Net is more common to use xml configuration files instead of INI files. And there is an API to handle it.

http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings.aspx#Y432

best regards.
// Get the AppSettings section.        
// This function uses the AppSettings property
// to read the appSettings configuration 
// section.
public static void ReadAppSettings()
{
  try
  {
    // Get the AppSettings section.
    NameValueCollection appSettings =
       ConfigurationManager.AppSettings;

    // Get the AppSettings section elements.
    Console.WriteLine();
    Console.WriteLine("Using AppSettings property.");
    Console.WriteLine("Application settings:");

    if (appSettings.Count == 0)
    {
      Console.WriteLine("[ReadAppSettings: {0}]",
      "AppSettings is empty Use GetSection command first.");
    }
    for (int i = 0; i < appSettings.Count; i++)
    {
      Console.WriteLine("#{0} Key: {1} Value: {2}",
        i, appSettings.GetKey(i), appSettings[i]);
    }
  }
  catch (ConfigurationErrorsException e)
  {
    Console.WriteLine("[ReadAppSettings: {0}]",
        e.ToString());
  }
}

Open in new window

0
 

Author Closing Comment

by:martin05
ID: 35067135
I got 2 different solutions to my problem .
Both will work so splitting the points

Thanks for your help guys
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

776 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