Mega Code Archive

 
Categories / C# Tutorial / Windows
 

Reading INI file

using System; using System.Runtime.InteropServices; using System.Text; class MainClass {     // Declare the unmanaged functions.     [DllImport("kernel32.dll", EntryPoint = "GetPrivateProfileString")]     private static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName);     static void Main(string[] args)     {         string section = "SampleSection"         string key = "Key1";         string filename = "\\initest.ini";         int chars = 256;         StringBuilder buffer = new StringBuilder(chars);         string sDefault = "";         if (GetPrivateProfileString(section, key, sDefault, buffer, chars, filename) != 0)         {             Console.WriteLine("Value of Key1 in [SampleSection] is: " + buffer.ToString());         }         else         {             Console.WriteLine("Value of Key1 in [SampleSection] is: " + null);         }     } }