using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace Sign_plate_management_system
{
public static class IniFileUtils
{
public static string FilePath { get; set; }
/// <summary>
/// 读操作:
/// </summary>
/// <param name="section">节</param>
/// <param name="key">键</param>
/// <param name="defVal">未读取到的默认值</param>
/// <param name="value">读取到的值,如果该key不存在则返回空值</param>
/// <param name="size">值的大小</param>
/// <param name="filePath">文件路径</param>
/// <returns></returns>
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string value, StringBuilder retVal, int size, string filePath);
/// <summary>
/// 写操作:
/// </summary>
/// <param name="section">节</param>
/// <param name="key">键</param>
/// <param name="val">值</param>
/// <param name="filePath">文件路径</param>
/// <returns></returns>
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string value, string filePath);
/// <summary>
/// 验证文件是否存在
/// </summary>
/// <returns>布尔值</returns>
public static bool ExistFile(string filepath)
{
return File.Exists(filepath);
}
/// <summary>
/// 验证文件是否存在
/// </summary>
/// <returns>布尔值</returns>
public static bool ExistFile()
{
return File.Exists(FilePath);
}
/// <summary>
/// 读取int文件
/// </summary>
/// <param name="section">节</param>
/// <param name="key">键</param>
/// <param name="defvalue">未读取到值时的默认值</param>
/// <param name="filepath">文件路径</param>
/// <returns>非零表示成功,零表示失败</returns>
public static string ReadIni(string section, string key, string defvalue, string filepath)
{
StringBuilder retvalue = new StringBuilder();
GetPrivateProfileString(section, key, defvalue, retvalue, 256, filepath);
return retvalue.ToString();
}
/// <summary>
/// 读取int文件
/// </summary>
/// <param name="section">节</param>
/// <param name="key">键</param>
/// <param name="defvalue">未读取到值时的默认值</param>
/// <param name="filepath">文件路径</param>
/// <returns>键值</returns>
public static string ReadIni(string section, string key, string defvalue)
{
StringBuilder retvalue = new StringBuilder(1024);
GetPrivateProfileString(section, key, defvalue, retvalue, 1024, FilePath);
return retvalue.ToString();
}
/// <summary>
/// 写入int文件
/// </summary>
/// <param name="section">节</param>
/// <param name="key">键</param>
/// <param name="value">值</param>
/// <param name="filepath">文件路径</param>
/// <returns></returns>
public static long WriteIni(string section, string key, string value, string filepath)
{
return WritePrivateProfileString(section, key, value, filepath);
}
/// <summary>
/// 写入int文件
/// </summary>
/// <param name="section">节</param>
/// <param name="key">键</param>
/// <param name="value">值</param>
/// <returns></returns>
public static long WriteIni(string section, string key, string value)
{
return WritePrivateProfileString(section, key, value, FilePath);
}
/// <summary>
/// 删除节
/// </summary>
/// <param name="section">节</param>
/// <param name="filepath">文件路径</param>
/// <returns></returns>
public static long DeleteSection(string section, string filepath)
{
return WritePrivateProfileString(section, null, null, filepath);
}
/// <summary>
/// 删除节
/// </summary>
/// <param name="section">节</param>
/// <returns></returns>
public static long DeleteSection(string section)
{
return WritePrivateProfileString(section, null, null ,FilePath);
}
/// <summary>
/// 删除键
/// </summary>
/// <param name="section">节</param>
/// <param name="key">键</param>
/// <param name="filepath">文件路径</param>
/// <returns></returns>
public static long DeleteKey(string section, string key, string filepath)
{
return WritePrivateProfileString(section, key, null, filepath);
}
/// <summary>
/// 删除键
/// </summary>
/// <param name="section">节</param>
/// <param name="key">键</param>
/// <returns></returns>
public static long DeleteKey(string section, string key)
{
return WritePrivateProfileString(section, key, null, FilePath);
}
}
}