1. Core class file INIFILE.cs code
1///<summary>
2///INIFILE Operation class
3///</summary>
4 public class INIFILE
5 {
6 [DllImport ("kernel32")]
7 private static extern long WritePrivateProfileString (string section,string key,string val,string filePath);
8
9 [DllImport ("kernel32")]
The private static extern int getprivateprofilestring (string section,string key,string def, StringBuilder retval,int size, String filePath);
11
12//The file path to access
private string strFilePath;
14
public string FilePath
16 {
strFilePath {return;}
set {strFilePath = value;}
19}
20
Public INIFILE ()
22 {
23}
24
INIFILE Public (String strFilePath)
26 {
This.strfilepath = strFilePath;
28}
29
public void WriteValue (string strsection,string strkey,string strvalue)
31 {
if (filepath.length = 0)
33 {
throw new Exception ("No Path set");
35}
WritePrivateProfileString (Strsection, strkey, strvalue, this.) FilePath);
37}
38
ReadValue string (string strsection,string strkey)
40 {
if (filepath.length = 0)
42 {
throw new Exception ("No Path set");
44}
StringBuilder sb = new StringBuilder ();
the int i = getprivateprofilestring (strsection, Strkey, "", SB, 255, this.) FilePath);
Return SB. ToString ();
48}
49}
2. Background Call file INIFile.aspx.cs code
1 protected void Page_Load(object sender, EventArgs e)
2 {
3 //Read
4 INIFILE ini = new INIFILE();
5 ini.FilePath = Request.PhysicalApplicationPath + "ini.ini";
6 string strReturnValue = ini.ReadValue("Annabelle", "Time");
7 Response.Write(strReturnValue);
8
9 //Write
10 INIFILE ini = new INIFILE();
11 ini.FilePath = Request.PhysicalApplicationPath + "ini.ini";
12 string strReturnValue = ini.ReadValue("Annabelle", "Time");
13 Response.Write(strReturnValue);
14 ini.WriteValue("Annabelle", "Time", "0");
15 strReturnValue = ini.ReadValue("Annabelle", "Time");
16 Response.Write(strReturnValue);
17 }