欢迎访问!
您的支持是我们不断前行的动力!

C# INI文件操作类

C#读INI文件、写INI文件、删除INI节点、删除INI键

//API函数声明
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32")]
private static extern long GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
/// <summary>
/// 读INI文件
/// </summary>
/// <param name="Section">节点</param>
/// <param name="Key">键名称</param>
/// <param name="NoText"></param>
/// <param name="iniFilePath">ini文件路径</param>
/// <returns></returns>
public static string ReadIniData(string Section, string Key, string NoText, string iniFilePath)
{
    if (File.Exists(iniFilePath))
    {
        StringBuilder temp = new StringBuilder(10240);
        GetPrivateProfileString(Section, Key, NoText, temp, 10240, iniFilePath);
        return temp.ToString();
    }
    else
    {
        return String.Empty;
    }
}
/// <summary>
/// 写INI文件
/// </summary>
/// <param name="Section">节点</param>
/// <param name="Key">键名称</param>
/// <param name="Value">键值</param>
/// <param name="iniFilePath">ini文件路径</param>
/// <returns></returns>
public static bool WriteIniData(string Section, string Key, string Value, string iniFilePath)
{
    if (File.Exists(iniFilePath))
    {
        long OpStation = WritePrivateProfileString(Section, Key, Value, iniFilePath);
        if (OpStation == 0)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    else
    {
        return false;
    }
}
/// <summary>
/// 删除节点
/// </summary>
/// <param name="section">节点</param>
/// <param name="filePath">ini文件路径</param>
/// <returns></returns>
public static long DeleteSection(string section, string filePath)
{
    return WritePrivateProfileString(section, null, null, filePath);
}
/// <summary>
/// 删除键
/// </summary>
/// <param name="section">节点</param>
/// <param name="key">键名称</param>
/// <param name="filePath">ini文件路径</param>
/// <returns></returns>
public static long DeleteKey(string section, string key, string filePath)
{
    return WritePrivateProfileString(section, key, null, filePath);
}
赞(0) 打赏
转载请注明出处,未注明出处不得转载,蜘蛛侠 » C# INI文件操作类
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

蜘蛛侠,您身边的爬虫专家!

觉得文章有用就打赏一下文章作者吧

支付宝扫一扫打赏

微信扫一扫打赏