ASP.net 2.0 read configuration file [INI] (sample code download)

Source: Internet
Author: User
Tags ini
asp.net| Example | download

(i). function

Action profile [*.ini] class

(ii). Code

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")]
A private static extern int getprivateprofilestring (string section,string key,string def, StringBuilder retval,int si Ze,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
INIFILE ini = new INIFILE ();
One INI. FilePath = Request.physicalapplicationpath + "Ini.ini";
A string strreturnvalue = ini. ReadValue ("Annabelle", "Time");
Response.Write (Strreturnvalue);
INI. WriteValue ("Annabelle", "Time", "0");
Strreturnvalue = ini. ReadValue ("Annabelle", "Time");
Response.Write (Strreturnvalue);
17}
(iii). Sample code Download

Http://www.cnblogs.com/Files/ChengKing/ReadSettingFile.rar




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.