首页 > 编程语言 >c#读取INI文件和向ini文件写入数据

c#读取INI文件和向ini文件写入数据

时间:2023-02-27 21:09:33浏览次数:39  
标签:文件 string c# DllImport int static ini public 读取


//读取INI文件

[DllImport("kernel32")]
public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

//向ini文件写入数据

[DllImport("kernel32")]
public static extern long WritePrivateProfileString(string mpAppName, string mpkeyName,
string mpDefault, string mpFileName);

//定时关机

[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
public static extern bool ExitWindowsEx(int uFlags, int dwReserved);

//关闭,重启系统拥有所有权限

[DllImport("ntdll.dll", ExactSpelling = true, SetLastError = true)]
public static extern bool RtlAdjustPrivilege(int htok, bool disall, bool newst, ref int len);
/// <summary>
/// 从ini中读取指定的值
/// </summary>
/// <param name="section">ini的节点</param>
/// <param name="key">节点下的项目</param>
/// <param name="def">没有找到时返回默认值</param>
/// <param name="filePath">要读取的文件路径</param>
/// <returns>返回要读取的节点内容</returns>
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();
}

标签:文件,string,c#,DllImport,int,static,ini,public,读取
From: https://blog.51cto.com/JohnsonJu/6089152

相关文章