您的位置:首页 > 编程语言 > ASP

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

2008-05-30 16:20 946 查看
转自:http://blog.csdn.net/ChengKing/archive/2007/01/05/1475115.aspx

(一). 功能
操作配置文件[*.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")]

private static extern int GetPrivateProfileString(string section,string key,string def, StringBuilder retVal,int size,string filePath);

//要访问的文件路径

private string strFilePath;

public string FilePath

{

get { return strFilePath; }

set { strFilePath = value; }

}

public INIFILE()

{

}

public INIFILE( string strFilePath )

{

this.strFilePath = strFilePath;

}

public void WriteValue(string strSection,string strKey,string strValue)

{

if (FilePath.Length == 0)

{

throw new Exception("没有设置路径");

}

WritePrivateProfileString(strSection, strKey, strValue, this.FilePath);

}

public string ReadValue(string strSection,string strKey)

{

if (FilePath.Length == 0)

{

throw new Exception("没有设置路径");

}

StringBuilder sb = new StringBuilder();

int i = GetPrivateProfileString(strSection, strKey, "", sb, 255, this.FilePath);

return sb.ToString();

}

}


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

INIFILE ini = new INIFILE();

ini.FilePath = Request.PhysicalApplicationPath + "ini.ini";

string strReturnValue = ini.ReadValue("Annabelle", "Time");

Response.Write(strReturnValue);

ini.WriteValue("Annabelle", "Time", "0");

strReturnValue = ini.ReadValue("Annabelle", "Time");

Response.Write(strReturnValue);

}


(三). 示例代码下载

http://files.cnblogs.com/ChengKing/ReadSettingFile.rar
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: