读取ini文件
1.ini文件:
[Update]
Version=2.2.0228.001
Recommend=1
VersionMemo=1.aasdfadfsafdfafd|2.YYYYYYY|3.ZZZZZZZZ
2.读取ini的cs类(单独建一个 封装读取)
using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
using System.Runtime.InteropServices;
/*
System.Runtime.InteropServices提供了相应的类或者方法来支持托管/非托管模块间的互相调用。
System.Runtime.InteropServices中几个比较重要的类:
DllImportAttribute : 该类提供对非托管动态链接库进行引用的方法,并告诉我们的编译器该程序的静态入口点是非托管的动态连接库,它的静态属性提供了对非托管动态链接库进行调用所必需的信息,作为最基本的要求,该类应该定义提供调用的非托管动态链接库的名称。成员详细信息
*/
namespace zkhis_update_remind
{
public class INIFile {
public string path;
public INIFile(string INIPath)
{
path = INIPath;
}
[DllImport("kernel32")]
/*
这叫引入kernel32.dll这个动态连接库。
这个动态连接库里面包含了很多WindowsAPI函数,如果你想使用这面的函数,就需要这么引入。举个例子:
[DllImport("kernel32.dll")]
private static extern void 函数名(参数,[参数]);
函数名就是一个属于kernel32.dll里的一个函数。完了你就可以用那个函数了。
kernel32.dll调用kernel32.dll这个DLL里面的API接口!
*/
private static extern long WritePrivateProfileString(string section,
string key, string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section,
string key, string def, StringBuilder retVal, int size, string filePath);
public void IniWriteValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, this.path);
}
public string IniReadValue(string Section, string Key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path);
return temp.ToString();
}
}
}
3.读取(窗体。或控制台。。。)
INIFile i = new INIFile(Server.MapPath("zkhis-update-config.ini"));
string Version=i.IniReadValue("Update","Version");
int Recommend = Convert.ToInt32(i.IniReadValue("Update", "Recommend"));
string VersionMemo = i.IniReadValue("Update", "VersionMemo");
INIModel ini = new INIModel()
{
Version =Version,
Recommend=Recommend,
VersionMemo=VersionMemo
};

浙公网安备 33010602011771号