ASP.net 2.0 read the configuration File Sample code

Source: Internet
Author: User
Tags ini int size

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 }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.