C#读取.ini文件

在这里插入图片描述
以上是ini文件中的内容
class ReadFile
{
[DllImport(“kernel32”)] //引入“shell32.dll”API文件
public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
///
/// 从INI文件中读取指定节点的内容
///
/// INI节点
/// 节点下的项
/// 没有找到内容时返回的默认值
/// 要读取的INI文件
/// 读取的节点内容
public static string GetIniFileString(string section, string key, string def, string filePath)
{
StringBuilder temp = new StringBuilder(1024);
GetPrivateProfileString(section, key, def, temp, 1024, filePath);
return temp.ToString();
}
}

比如要读取的是当前工程目录下的DEBUG文件下的Express.ini内容:

string strServer = ReadFile.GetIniFileString(“DataBase”, “Server”, “”, Application.StartupPath + “\Express.ini”);

猜你喜欢

转载自blog.csdn.net/qq_35439171/article/details/82940315