Introduction:
In this article,i am going to explain about how to read .INI file entries/values using asp.net c#.
Main:
Normally in visual basic 6.0 and above versions we using kernal.dll for reading/writing .INI file config vales.
In asp.net also we can use the same dll for reading .INI file ertries.
What is kernal.dll?
The Kernel32.dll file handles memory management, input/output operations and interrupts. When you start Windows, Kernel32.dll is loaded into a protected memory space so that other programs do not take it over.You can easily download kernal dll in microsoft websites.
class ReadingINIFile
{
private string INIfilePath;
[DllImport("kernel32")]
private static extern long WriteINIentries(string section,
string key,
string val,
string INIfilePath);
[DllImport("kernel32")]
private static extern int GetINIentries(string section,
string key,
string def,
StringBuilder retVal,
int size,
string INIfilePath);
public ReadingINIFile(string INIfilePath)
{
this.INIfilePath = INIfilePath;
}
public void Write(string section, string key, string value)
{
WriteINIentries(section, key, value.ToLower(), this.INIfilePath);
}
public string Read(string section, string key)
{
StringBuilder SB = new StringBuilder(255);
int i = GetINIentries(section, key, "", SB, 255, this.INIfilePath);
return SB.ToString();
}
public string INIfilePath
{
get { return this.INIfilePath; }
set { this.INIfilePath = value; }
}
}
ReadingINIFile local_ini = new ReadingINIFile("C:\\Test.ini");
local_ini .Write("Credentials","Password","david");
class ReadingINIFile { private string INIfilePath; [DllImport("kernel32")] private static extern long WriteINIentries(string section, string key, string val, string INIfilePath); [DllImport("kernel32")] private static extern int GetINIentries(string section, string key, string def, StringBuilder retVal, int size, string INIfilePath); public ReadingINIFile(string INIfilePath) { this.INIfilePath = INIfilePath; } public void Write(string section, string key, string value) { WriteINIentries(section, key, value.ToLower(), this.INIfilePath); } public string Read(string section, string key) { StringBuilder SB = new StringBuilder(255); int i = GetINIentries(section, key, "", SB, 255, this.INIfilePath); return SB.ToString(); } public string INIfilePath { get { return this.INIfilePath; } set { this.INIfilePath = value; } } } ReadingINIFile local_ini = new ReadingINIFile("C:\\Test.ini"); local_ini .Write("Credentials","Password","david"); |
Conclusion:
Hope this helps,
Happy Coding.
1 Comments.