If you would like learn how to read and write from and to INI files in your Visual CSharp .Net project follow the guide bellow.
Reading and Writing INI Files
To write to your settings INI files you will first need to import the correct API functions, GetPrivateProfileString and WriteProfileString, for the Kernel32 system library. Place this code after your references above your namespace:
[DllImport("kernel32.dll", EntryPoint = "GetPrivateProfileString")]
private static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName);
[DllImport("kernel32.dll", EntryPoint = "WritePrivateProfileString")]
private static extern bool WritePrivateProfileString(string lpAppName, string lpKeyName, string lpString, string lpFileName);
With the proper API functions included you are able to create an INI reading method. Construct a public string void called GetINI with this source code:
public static string GetINI(string INISECTION, string INIKEY, string INIFILE)
{
int chrs = 256;
StringBuilder bffer = new StringBuilder(chrs);
string sDFLT = "";
if (GetPrivateProfileString(INISECTION, INIKEY, sDFLT, bffer, chrs, INIFILE) != 0)
{
return bffer.ToString();
}
else
{
return null;
}
}
Create another public void named WriteINI using the code provided below.
public void WriteINI(string INISECTION, string INIKEY, string NEWVALUE, string INIFILE)
{
WritePrivateProfileString(INISECTION, INIKEY, NEWVALUE, INIFILE);
}
Conclusion
Now that you have create both a reading and a writing INI function you can simply call either one to write or read desire your desired value. Here is an example of the writing function:
WriteINI("mySection", "myKey", "myValue", "C:\\myINI.ini");
This code will write the value "myValue" to the key "myKey" in the section "mySection" found in the INI file located at "C:\myINI.ini".
And the reading function:
String strTemp = "";
strTemp = GetINI("mySection", "myKey", "C:\\myINI.ini");
The above example will store the value of the key "myKey" found in the section "mySection" from the INI file located at "C:\myINI.ini" into strTemp.
Published by James Cloud
I like to program and do basically anything that has to do with technology and computers. View profile
- 10 Tips on How to Write a Consignment Agreement10 Tips On How to Write a Consignment Agreement
- Guide on How to Write an Employee ManualArticle on how to write an employee manual
- 5 Tips on How to Stay Motivated to WriteGuide to help freelance writers to help stay motivated to write
- How to Write SongsUsing basic music theory knowledge about scales and progressions, songwriters will find many new chords to write songs with. Because they come from the same key they will have notes in common and sound good together....
- Chinese Face Reading: What Your Laugh Lines IndicateChinese face reading is an ancient method of evaluating personalities and predicting your luck. The laugh lines are considered one of the signs of longevity, but only if you have the right type.
- Raising Reading Children
- Tips to Getting a Great Psychic Reading
- How to Help Your ADD/ADHD Child with Reading
- Chinese Face Reading: What Your Lips Say About Your Fortunes
- Chinese Face Reading: The Jaw and What it Means
- Keep Children Reading Through the Summer
- In Business You Need to Write



