Solved

Read INI file

Posted on 2011-03-03
5
675 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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 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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

759 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

21 Experts available now in Live!

Get 1:1 Help Now