首页 > 编程 > .NET > 正文

ASP.NET 2.0 读取配置文件[INI](示例代码下载)

2024-07-10 13:06:07
字体:
来源:转载
供稿:网友

(一). 功能

         操作配置文件[*.ini]类

(二). 代码

     1. 核心类文件 inifile.cs 代码

 1 /// <summary>
 2 /// inifile 操作类
 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")]
10     private static extern int getprivateprofilestring(string section,string key,string def, stringbuilder retval,int size,string filepath);
11
12     //要访问的文件路径
13     private string strfilepath;
14
15     public string filepath
16     {
17       get { return strfilepath; }
18       set { strfilepath = value; }
19     }
20
21     public inifile()
22     {    
23     }
24     
25     public inifile( string strfilepath )
26     {   
27         this.strfilepath = strfilepath;
28     }    
29
30     public void writevalue(string strsection,string strkey,string strvalue)
31     {
32         if (filepath.length == 0)
33         {
34             throw new exception("没有设置路径");
35         }
36         writeprivateprofilestring(strsection, strkey, strvalue, this.filepath);       
37     }
38      
39     public string readvalue(string strsection,string strkey)
40     {
41         if (filepath.length == 0)
42         {
43             throw new exception("没有设置路径");
44         }
45         stringbuilder sb = new stringbuilder();
46         int i = getprivateprofilestring(strsection, strkey, "", sb, 255, this.filepath);
47         return sb.tostring();
48     }
49 }
2. 后台调用文件 inifile.aspx.cs 代码

 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     }
(三). 示例代码下载

        http://www.cnblogs.com/files/chengking/readsettingfile.rar


 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表