njcit/NJCIT/OperateIniFile.cs
2019-09-14 20:12:26 +08:00

73 lines
2.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace NJCIT
{
public class OperateIniFile
{
#region API函数声明
[DllImport("kernel32")] //返回0表示失败非0为成功
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);
#endregion
/// <summary>
/// 读Ini文件
/// </summary>
/// <param name="Section">[]内的段落名</param>
/// <param name="Key">key</param>
/// <param name="NoText"></param>
/// NoText对应API函数的def参数它的值由用户指定是当在配置文件中没有找到具体的Value时就用NoText的值来代替。可以为空
/// <param name="iniFilePath">ini配置文件的路径加ini文件名</param>
/// <returns></returns>
#region Ini文件
public static string ReadIniData(string Section, string Key, string NoText, string iniFilePath)
{
if (File.Exists(iniFilePath))
{
StringBuilder temp = new StringBuilder(1024);
GetPrivateProfileString(Section, Key, NoText, temp, 1024, iniFilePath);
return temp.ToString();
}
else
{
return String.Empty;
}
}
#endregion
#region Ini文件
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;
}
}
#endregion
}
}