C#如何读取写在ini中的数据库连接字符串
2个回答
展开全部
给你一个类,可以直接读取INI文件中东西
public class IniFile
{
/// <summary>
/// ini文件名称(带路径)
/// </summary>
public string filePath;
//声明读写INI文件的API函数
[DllImport("kernel32")]
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);
/// <summary>
/// 类的构造函数
/// </summary>
/// <param name="INIPath">INI文件名</param>
public IniFile(string INIPath)
{
filePath = INIPath;
}
/// <summary>
/// 写INI文件
/// </summary>
/// <param name="Section">Section</param>
/// <param name="Key">Key</param>
/// <param name="value">value</param>
public void WriteInivalue(string Section, string Key, string value)
{
WritePrivateProfileString(Section, Key, value, this.filePath);
}
/// <summary>
/// 读取INI文件指定部分
/// </summary>
/// <param name="Section">Section</param>
/// <param name="Key">Key</param>
/// <returns>String</returns>
public string ReadInivalue(string Section, string Key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.filePath);
return temp.ToString();
}
}
public class IniFile
{
/// <summary>
/// ini文件名称(带路径)
/// </summary>
public string filePath;
//声明读写INI文件的API函数
[DllImport("kernel32")]
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);
/// <summary>
/// 类的构造函数
/// </summary>
/// <param name="INIPath">INI文件名</param>
public IniFile(string INIPath)
{
filePath = INIPath;
}
/// <summary>
/// 写INI文件
/// </summary>
/// <param name="Section">Section</param>
/// <param name="Key">Key</param>
/// <param name="value">value</param>
public void WriteInivalue(string Section, string Key, string value)
{
WritePrivateProfileString(Section, Key, value, this.filePath);
}
/// <summary>
/// 读取INI文件指定部分
/// </summary>
/// <param name="Section">Section</param>
/// <param name="Key">Key</param>
/// <returns>String</returns>
public string ReadInivalue(string Section, string Key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.filePath);
return temp.ToString();
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询