class SParam //SystemParam
{
public static string basePath = @"InitParamter.ini";
private static string systemINIPath = Path.Combine(Directory.GetCurrentDirectory(), SFC.ST_Directory.parameter, basePath);//系统参数ini
struct ST_Directory
{
public const string parameter = @"/parameter";//ini文件路径
}
/// <summary>
/// 信息读取
/// </summary>
public static void SaveSystemParamIni()
{
if (!System.IO.Directory.Exists(Directory.GetCurrentDirectory() + SParam.ST_Directory.parameter))
{
System.IO.Directory.CreateDirectory(Directory.GetCurrentDirectory() + SParam.ST_Directory.parameter);
}
if (!File.Exists(systemINIPath))
{
SystemParamIni();
WriteSystemParam();
}
else
{
ReadSystemParam();
}
}
#region 串口
public struct SystemCom //串口
{
/// <summary>
/// 串口端口
/// </summary>
public string PortName;
/// <summary>
/// 波特率
/// </summary>
public string BaudRate;
/// <summary>
/// 数据位
/// </summary>
public string DataBits;
/// <summary>
/// 校验位
/// </summary>
public string ParityReplace;
/// <summary>
/// 停止位
/// </summary>
public string StopBits;
/// <summary>
/// 启用/禁用扫码
/// </summary>
public bool isUseScan;
}
public static SystemCom LeftScanCom,RightScanCom;//扫码枪COM
/// <summary>
/// 串口1属性
/// </summary>
public static SystemCom LeftScanComSet
{
get { return LeftScanCom; }
set { LeftScanCom = value; }
}
/// <summary>
/// 串口2属性
/// </summary>
public static SystemCom RightScanComSet
{
get { return RightScanCom; }
set { RightScanCom = value; }
}
#endregion
#region CCD
public struct CCD
{
/// <summary>
/// CCDIP
/// </summary>
public string ccdIP;
/// <summary>
/// CCDPORT
/// </summary>
public string ccdPort;
public bool isUseCCD;
}
public static CCD LeftCCD,RightCCD;
/// <summary>
/// 网口1属性
/// </summary>
public static CCD LeftCCDSet
{
get { return LeftCCD; }
set { LeftCCD = value; }
}
/// <summary>
/// 网口2属性
/// </summary>
public static CCD RightCCDSet
{
get { return RightCCD; }
set { RightCCD = value; }
}
#endregion
#region SFC
public struct SFCMes
{
//SFC设置
public string sfcLocalIP;
public string sfcServerIP;
public string sfcServerPort;
public bool isUseSFC;
//SMT信息设置
public string smtStageCode;
public string smtLine;
public string smtWorkStation;
public string smtUserID;
//SFC信息设置
public string sfcMesMachineName;
public string sfcMesMachineID;
public string sfcMesProductType;
public string sfcMesProductArea;
public string sfcMesMO;
//其他设置
public string logPath;
public string CT_LineDownPath;
public string softWareVersion;
}
public static SFCMes Sfc;
/// <summary>
/// SFC属性
/// </summary>
public static SFCMes SfcSet
{
get { return Sfc; }
set { Sfc = value; }
}
#endregion
#region SystemParam设置
private static void SystemParamIni()
{ //SFC设置
Sfc.sfcLocalIP = "127.1.1.1";
Sfc.sfcServerIP = "127.1.1.1";
Sfc.sfcServerPort = "2000";
Sfc.isUseSFC = true;
SMT相关信息设置
Sfc.smtStageCode = "AF";
Sfc.smtLine = "123";
Sfc.smtWorkStation = "TB3";
Sfc.smtUserID = "Kttach";
//SFC相关信息设置
Sfc.sfcMesMachineName = "LinkBar";
Sfc.sfcMesMachineID = "TB-01-02-LI";
Sfc.sfcMesProductType = "x1777";
Sfc.sfcMesProductArea = "ISO";
Sfc.sfcMesMO = "123";
//日志路径
Sfc.logPath ="D:\\LinkBar_WorkData";
Sfc.CT_LineDownPath ="D:\\Data";
//启用扫码
LeftScanCom.isUseScan = true;
RightScanCom.isUseScan = true;
//相机1
LeftCCD.ccdIP = "127.0.0.1";
LeftCCD.ccdPort = "7000";
LeftCCD.isUseCCD = true;
//相机2
RightCCD.ccdIP = "127.0.0.1";
RightCCD.ccdPort = "8000";
//串口1
LeftScanCom.PortName = "COM1";
LeftScanCom.BaudRate = "115200";
LeftScanCom.DataBits = "8";
LeftScanCom.ParityReplace = "Even";
LeftScanCom.StopBits = "None";
//串口2
RightScanCom.PortName = "COM2";
RightScanCom.BaudRate = "115200";
RightScanCom.DataBits = "8";
RightScanCom.ParityReplace = "Even";
RightScanCom.StopBits = "None";
Sfc.softWareVersion = "Suntek_LB_V3.0";
}
public static void WriteSystemParam()
{
//sfc设置
IniOP.INIWriteValue(systemINIPath, SFC.STC_Ini.SectionSfc, SFC.STC_Ini.KeySfcLocalIp, Sfc.sfcLocalIP); //SFC.STC_Ini.SectionSfc是结构体
IniOP.INIWriteValue(systemINIPath, SFC.STC_Ini.SectionSfc, SFC.STC_Ini.KeySfcServiceIp, Sfc.sfcServerIP);
IniOP.INIWriteValue(systemINIPath, SFC.STC_Ini.SectionSfc, SFC.STC_Ini.KeySfcServicePort, Sfc.sfcServerPort);
IniOP.INIWriteValue(systemINIPath, SFC.STC_Ini.SectionSfc, SFC.STC_Ini.KeyActiveSfc, Sfc.isUseSFC ? SFC.STC_Ini.ValueActiveSfc : SFC.STC_Ini.ValueInactiveSfc);
SMT相关信息设置
IniOP.INIWriteValue(systemINIPath, SFC.STC_Ini.SectionSmt, SFC.STC_Ini.KeyStageCode, Sfc.smtStageCode);
IniOP.INIWriteValue(systemINIPath, SFC.STC_Ini.SectionSmt, SFC.STC_Ini.KeyLine, Sfc.smtLine);
IniOP.INIWriteValue(systemINIPath, SFC.STC_Ini.SectionSmt, SFC.STC_Ini.KeyWorkStation, Sfc.smtWorkStation);
IniOP.INIWriteValue(systemINIPath, SFC.STC_Ini.SectionSmt, SFC.STC_Ini.KeyUserID, Sfc.smtUserID);
//SFC相关信息设置
IniOP.INIWriteValue(systemINIPath, SFC.STC_Ini.SectionAe, SFC.STC_Ini.KeyAeName, Sfc.sfcMesMachineName);
IniOP.INIWriteValue(systemINIPath, SFC.STC_Ini.SectionAe, SFC.STC_Ini.KeyAeId, Sfc.sfcMesMachineID);
IniOP.INIWriteValue(systemINIPath, SFC.STC_Ini.SectionAe, SFC.STC_Ini.KeyProduct, Sfc.sfcMesProductType);
IniOP.INIWriteValue(systemINIPath, SFC.STC_Ini.SectionAe, SFC.STC_Ini.KeyProductPosition, Sfc.sfcMesProductArea);
IniOP.INIWriteValue(systemINIPath, SFC.STC_Ini.SectionAe, SFC.STC_Ini.KeyProductMO, Sfc.sfcMesMO);
//日志路径
IniOP.INIWriteValue(systemINIPath, SFC.STC_Ini.DataLogPath, SFC.STC_Ini.DataLog, Sfc.logPath);
IniOP.INIWriteValue(systemINIPath, SFC.STC_Ini.DataLogPath, SFC.STC_Ini.CT_LINEDOWN, Sfc.CT_LineDownPath);
//启用扫码
IniOP.INIWriteValue(systemINIPath, "UseScan", "LeftCB_USEScan", LeftScanCom.isUseScan ? SFC.STC_Ini.CB_LeftUseScan : SFC.STC_Ini.CB_LeftNoUseScan);
IniOP.INIWriteValue(systemINIPath, "UseScan", "RightCB_USEScan", RightScanCom.isUseScan ? SFC.STC_Ini.CB_RightUseScan : SFC.STC_Ini.CB_RightNoUseScan);
//相机1
IniOP.INIWriteValue(systemINIPath, SFC.STC_Ini.SectionCcd, SFC.STC_Ini.LeftCcdcIp, LeftCCD.ccdIP);
IniOP.INIWriteValue(systemINIPath, SFC.STC_Ini.SectionCcd, SFC.STC_Ini.LeftCcdcPort, LeftCCD.ccdPort);
IniOP.INIWriteValue(systemINIPath, SFC.STC_Ini.SectionCcd, SFC.STC_Ini.CCDSet, LeftCCD.isUseCCD?SFC.STC_Ini.UseCCD:SFC.STC_Ini.UnUseCCD);
//相机2
IniOP.INIWriteValue(systemINIPath, SFC.STC_Ini.SectionCcd, SFC.STC_Ini.RightCcdcIp, RightCCD.ccdIP);
IniOP.INIWriteValue(systemINIPath, SFC.STC_Ini.SectionCcd, SFC.STC_Ini.RightCcdcPort, RightCCD.ccdPort);
//串口1
IniOP.INIWriteValue(systemINIPath, SFC.STC_Ini.SectionScan, SFC.STC_Ini.LeftPortName, LeftScanCom.PortName);
IniOP.INIWriteValue(systemINIPath, SFC.STC_Ini.SectionScan, SFC.STC_Ini.LeftBaudRate, LeftScanCom.BaudRate);
IniOP.INIWriteValue(systemINIPath, SFC.STC_Ini.SectionScan, SFC.STC_Ini.LeftBytesToRead, LeftScanCom.DataBits);
IniOP.INIWriteValue(systemINIPath, SFC.STC_Ini.SectionScan, SFC.STC_Ini.LeftParityReplace, LeftScanCom.ParityReplace);
IniOP.INIWriteValue(systemINIPath, SFC.STC_Ini.SectionScan, SFC.STC_Ini.LeftStopBits, LeftScanCom.StopBits);
//串口2
IniOP.INIWriteValue(systemINIPath, SFC.STC_Ini.SectionScan, SFC.STC_Ini.RightPortName, RightScanCom.PortName);
IniOP.INIWriteValue(systemINIPath, SFC.STC_Ini.SectionScan, SFC.STC_Ini.RightBaudRate, RightScanCom.BaudRate);
IniOP.INIWriteValue(systemINIPath, SFC.STC_Ini.SectionScan, SFC.STC_Ini.RightBytesToRead, RightScanCom.DataBits);
IniOP.INIWriteValue(systemINIPath, SFC.STC_Ini.SectionScan, SFC.STC_Ini.RightParityReplace, RightScanCom.ParityReplace);
IniOP.INIWriteValue(systemINIPath, SFC.STC_Ini.SectionScan, SFC.STC_Ini.RightStopBits, RightScanCom.StopBits);
//其他设置
IniOP.INIWriteValue(systemINIPath, SFC.STC_Ini.OtherSet, SFC.STC_Ini.softWareVersion, Sfc.softWareVersion);
}
public static void ReadSystemParam()
{
//SFC设置
Sfc.sfcLocalIP = IniOP.INIGetStringValue(systemINIPath, SFC.STC_Ini.SectionSfc, SFC.STC_Ini.KeySfcLocalIp, null);
Sfc.sfcServerIP = IniOP.INIGetStringValue(systemINIPath, SFC.STC_Ini.SectionSfc, SFC.STC_Ini.KeySfcServiceIp, null);
Sfc.sfcServerPort = IniOP.INIGetStringValue(systemINIPath, SFC.STC_Ini.SectionSfc, SFC.STC_Ini.KeySfcServicePort, null);
Sfc.isUseSFC = IniOP.INIGetStringValue(systemINIPath, SFC.STC_Ini.SectionSfc, SFC.STC_Ini.KeyActiveSfc, null).Equals(SFC.STC_Ini.ValueActiveSfc);
//SMT信息设置
Sfc.smtStageCode = IniOP.INIGetStringValue(systemINIPath, SFC.STC_Ini.SectionSmt, SFC.STC_Ini.KeyStageCode, null);
Sfc.smtLine = IniOP.INIGetStringValue(systemINIPath, SFC.STC_Ini.SectionSmt, SFC.STC_Ini.KeyLine, null);
Sfc.smtWorkStation = IniOP.INIGetStringValue(systemINIPath, SFC.STC_Ini.SectionSmt, SFC.STC_Ini.KeyWorkStation, null);
Sfc.smtUserID = IniOP.INIGetStringValue(systemINIPath, SFC.STC_Ini.SectionSmt, SFC.STC_Ini.KeyUserID, null);
//SFC信息设置
Sfc.sfcMesMachineName = IniOP.INIGetStringValue(systemINIPath, SFC.STC_Ini.SectionAe, SFC.STC_Ini.KeyAeName, null);
Sfc.sfcMesMachineID = IniOP.INIGetStringValue(systemINIPath, SFC.STC_Ini.SectionAe, SFC.STC_Ini.KeyAeId, null);
Sfc.sfcMesProductType = IniOP.INIGetStringValue(systemINIPath, SFC.STC_Ini.SectionAe, SFC.STC_Ini.KeyProduct, null);
Sfc.sfcMesProductArea = IniOP.INIGetStringValue(systemINIPath, SFC.STC_Ini.SectionAe, SFC.STC_Ini.KeyProductPosition, null);
Sfc.sfcMesMO = IniOP.INIGetStringValue(systemINIPath, SFC.STC_Ini.SectionAe, SFC.STC_Ini.KeyProductMO, null);
//日志路径
Sfc.logPath = IniOP.INIGetStringValue(systemINIPath, SFC.STC_Ini.DataLogPath, SFC.STC_Ini.DataLog, null);
Sfc.CT_LineDownPath = IniOP.INIGetStringValue(systemINIPath, SFC.STC_Ini.DataLogPath, SFC.STC_Ini.CT_LINEDOWN, null);
//启用扫码
LeftScanCom .isUseScan= IniOP.INIGetStringValue(systemINIPath, "UseScan", "LeftCB_USEScan", null).Equals(SFC.STC_Ini.CB_LeftUseScan);
RightScanCom.isUseScan = IniOP.INIGetStringValue(systemINIPath, "UseScan", "RightCB_USEScan", null).Equals(SFC.STC_Ini.CB_RightUseScan);
//CCD1
LeftCCD.ccdIP = IniOP.INIGetStringValue(systemINIPath, SFC.STC_Ini.SectionCcd, SFC.STC_Ini.LeftCcdcIp, null);
LeftCCD.ccdPort = IniOP.INIGetStringValue(systemINIPath, SFC.STC_Ini.SectionCcd, SFC.STC_Ini.LeftCcdcPort, null);
LeftCCD.isUseCCD = IniOP.INIGetStringValue(systemINIPath, SFC.STC_Ini.SectionCcd, SFC.STC_Ini.CCDSet, null).Equals(SFC.STC_Ini.UseCCD);
//CCD2
RightCCD.ccdIP = IniOP.INIGetStringValue(systemINIPath, SFC.STC_Ini.SectionCcd, SFC.STC_Ini.RightCcdcIp, null);
RightCCD.ccdPort = IniOP.INIGetStringValue(systemINIPath, SFC.STC_Ini.SectionCcd, SFC.STC_Ini.RightCcdcPort, null);
//串口1
LeftScanCom.PortName = IniOP.INIGetStringValue(systemINIPath, SFC.STC_Ini.SectionScan, SFC.STC_Ini.LeftPortName, null);
LeftScanCom.BaudRate = IniOP.INIGetStringValue(systemINIPath, SFC.STC_Ini.SectionScan, SFC.STC_Ini.LeftBaudRate, null);
LeftScanCom.DataBits = IniOP.INIGetStringValue(systemINIPath, SFC.STC_Ini.SectionScan, SFC.STC_Ini.LeftBytesToRead, null);
LeftScanCom.ParityReplace = IniOP.INIGetStringValue(systemINIPath, SFC.STC_Ini.SectionScan, SFC.STC_Ini.LeftParityReplace, null);
LeftScanCom.StopBits = IniOP.INIGetStringValue(systemINIPath, SFC.STC_Ini.SectionScan, SFC.STC_Ini.LeftStopBits, null);
//串口2
RightScanCom.PortName = IniOP.INIGetStringValue(systemINIPath, SFC.STC_Ini.SectionScan, SFC.STC_Ini.RightPortName, null);
RightScanCom.BaudRate = IniOP.INIGetStringValue(systemINIPath, SFC.STC_Ini.SectionScan, SFC.STC_Ini.RightBaudRate, null);
RightScanCom.DataBits = IniOP.INIGetStringValue(systemINIPath, SFC.STC_Ini.SectionScan, SFC.STC_Ini.RightBytesToRead, null);
RightScanCom.ParityReplace = IniOP.INIGetStringValue(systemINIPath, SFC.STC_Ini.SectionScan, SFC.STC_Ini.RightParityReplace, null);
RightScanCom.StopBits = IniOP.INIGetStringValue(systemINIPath, SFC.STC_Ini.SectionScan, SFC.STC_Ini.RightStopBits, null);
Sfc.softWareVersion = IniOP.INIGetStringValue(systemINIPath, SFC.STC_Ini.OtherSet, SFC.STC_Ini.softWareVersion, null);
}
#endregion
}
##INI文件保存类
public class IniOP
{
#region INI文件操作
/*
* 针对INI文件的API操作方法,其中的节点(Section)、键(KEY)都不区分大小写
* 如果指定的INI文件不存在,会自动创建该文件。
*
* CharSet定义的时候使用了什么类型,在使用相关方法时必须要使用相应的类型
* 例如 GetPrivateProfileSectionNames声明为CharSet.Auto,那么就应该使用 Marshal.PtrToStringAuto来读取相关内容
* 如果使用的是CharSet.Ansi,就应该使用Marshal.PtrToStringAnsi来读取内容
*
*/
#region API声明
/// <summary>
/// 获取所有节点名称(Section)
/// </summary>
/// <param name="lpszReturnBuffer">存放节点名称的内存地址,每个节点之间用\0分隔</param>
/// <param name="nSize">内存大小(characters)</param>
/// <param name="lpFileName">Ini文件</param>
/// <returns>内容的实际长度,为0表示没有内容,为nSize-2表示内存大小不够</returns>
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern uint GetPrivateProfileSectionNames(IntPtr lpszReturnBuffer, uint nSize, string lpFileName);
/// <summary>
/// 获取某个指定节点(Section)中所有KEY和Value
/// </summary>
/// <param name="lpAppName">节点名称</param>
/// <param name="lpReturnedString">返回值的内存地址,每个之间用\0分隔</param>
/// <param name="nSize">内存大小(characters)</param>
/// <param name="lpFileName">Ini文件</param>
/// <returns>内容的实际长度,为0表示没有内容,为nSize-2表示内存大小不够</returns>
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern uint GetPrivateProfileSection(string lpAppName, IntPtr lpReturnedString, uint nSize, string lpFileName);
/// <summary>
/// 读取INI文件中指定的Key的值
/// </summary>
/// <param name="lpAppName">节点名称。如果为null,则读取INI中所有节点名称,每个节点名称之间用\0分隔</param>
/// <param name="lpKeyName">Key名称。如果为null,则读取INI中指定节点中的所有KEY,每个KEY之间用\0分隔</param>
/// <param name="lpDefault">读取失败时的默认值</param>
/// <param name="lpReturnedString">读取的内容缓冲区,读取之后,多余的地方使用\0填充</param>
/// <param name="nSize">内容缓冲区的长度</param>
/// <param name="lpFileName">INI文件名</param>
/// <returns>实际读取到的长度</returns>
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern uint GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, [In, Out] char[] lpReturnedString, uint nSize, string lpFileName);
//另一种声明方式,使用 StringBuilder 作为缓冲区类型的缺点是不能接受\0字符,会将\0及其后的字符截断,
//所以对于lpAppName或lpKeyName为null的情况就不适用
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern uint GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, uint nSize, string lpFileName);
//再一种声明,使用string作为缓冲区的类型同char[]
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern uint GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, string lpReturnedString, uint nSize, string lpFileName);
/// <summary>
/// 将指定的键值对写到指定的节点,如果已经存在则替换。
/// </summary>
/// <param name="lpAppName">节点,如果不存在此节点,则创建此节点</param>
/// <param name="lpString">Item键值对,多个用\0分隔,形如key1=value1\0key2=value2
/// <para>如果为string.Empty,则删除指定节点下的所有内容,保留节点</para>
/// <para>如果为null,则删除指定节点下的所有内容,并且删除该节点</para>
/// </param>
/// <param name="lpFileName">INI文件</param>
/// <returns>是否成功写入</returns>
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)] //可以没有此行
private static extern bool WritePrivateProfileSection(string lpAppName, string lpString, string lpFileName);
/// <summary>
/// 将指定的键和值写到指定的节点,如果已经存在则替换
/// </summary>
/// <param name="lpAppName">节点名称</param>
/// <param name="lpKeyName">键名称。如果为null,则删除指定的节点及其所有的项目</param>
/// <param name="lpString">值内容。如果为null,则删除指定节点中指定的键。</param>
/// <param name="lpFileName">INI文件</param>
/// <returns>操作是否成功</returns>
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool WritePrivateProfileString(string lpAppName, string lpKeyName, string lpString, string lpFileName);
#endregion
#region 封装
/// <summary>
/// 读取INI文件中指定INI文件中的所有节点名称(Section)
/// </summary>
/// <param name="iniFile">Ini文件</param>
/// <returns>所有节点,没有内容返回string[0]</returns>
public static string[] INIGetAllSectionNames(string iniFile)
{
uint MAX_BUFFER = 32767; //默认为32767
string[] sections = new string[0]; //返回值
//申请内存
IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER * sizeof(char));
uint bytesReturned = IniOP.GetPrivateProfileSectionNames(pReturnedString, MAX_BUFFER, iniFile);
if (bytesReturned != 0)
{
//读取指定内存的内容
string local = Marshal.PtrToStringAuto(pReturnedString, (int)bytesReturned).ToString();
//每个节点之间用\0分隔,末尾有一个\0
sections = local.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries);
}
//释放内存
Marshal.FreeCoTaskMem(pReturnedString);
return sections;
}
/// <summary>
/// 获取INI文件中指定节点(Section)中的所有条目(key=value形式)
/// </summary>
/// <param name="iniFile">Ini文件</param>
/// <param name="section">节点名称</param>
/// <returns>指定节点中的所有项目,没有内容返回string[0]</returns>
public static string[] INIGetAllItems(string iniFile, string section)
{
//返回值形式为 key=value,例如 Color=Red
uint MAX_BUFFER = 32767; //默认为32767
string[] items = new string[0]; //返回值
//分配内存
IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER * sizeof(char));
uint bytesReturned = IniOP.GetPrivateProfileSection(section, pReturnedString, MAX_BUFFER, iniFile);
if (!(bytesReturned == MAX_BUFFER - 2) || (bytesReturned == 0))
{
string returnedString = Marshal.PtrToStringAuto(pReturnedString, (int)bytesReturned);
items = returnedString.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries);
}
Marshal.FreeCoTaskMem(pReturnedString); //释放内存
return items;
}
/// <summary>
/// 获取INI文件中指定节点(Section)中的所有条目的Key列表
/// </summary>
/// <param name="iniFile">Ini文件</param>
/// <param name="section">节点名称</param>
/// <returns>如果没有内容,反回string[0]</returns>
public static string[] INIGetAllItemKeys(string iniFile, string section)
{
string[] value = new string[0];
const int SIZE = 1024 * 10;
if (string.IsNullOrEmpty(section))
{
throw new ArgumentException("必须指定节点名称", "section");
}
char[] chars = new char[SIZE];
uint bytesReturned = IniOP.GetPrivateProfileString(section, null, null, chars, SIZE, iniFile);
if (bytesReturned != 0)
{
value = new string(chars).Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries);
}
chars = null;
return value;
}
/// <summary>
/// 读取INI文件中指定KEY的字符串型值
/// </summary>
/// <param name="iniFile">Ini文件</param>
/// <param name="section">节点名称</param>
/// <param name="key">键名称</param>
/// <param name="defaultValue">如果没此KEY所使用的默认值</param>
/// <returns>读取到的值</returns>
public static string INIGetStringValue(string iniFile, string section, string key, string defaultValue)
{
string value = defaultValue;
const int SIZE = 1024 * 10;
if (string.IsNullOrEmpty(section))
{
throw new ArgumentException("必须指定节点名称", "section");
}
if (string.IsNullOrEmpty(key))
{
throw new ArgumentException("必须指定键名称(key)", "key");
}
StringBuilder sb = new StringBuilder(SIZE);
uint bytesReturned = IniOP.GetPrivateProfileString(section, key, defaultValue, sb, SIZE, iniFile);
if (bytesReturned != 0)
{
value = sb.ToString();
}
sb = null;
return value;
}
/// <summary>
/// 在INI文件中,将指定的键值对写到指定的节点,如果已经存在则替换
/// </summary>
/// <param name="iniFile">INI文件</param>
/// <param name="section">节点,如果不存在此节点,则创建此节点</param>
/// <param name="items">键值对,多个用\0分隔,形如key1=value1\0key2=value2</param>
/// <returns></returns>
public static bool INIWriteItems(string iniFile, string section, string items)
{
if (string.IsNullOrEmpty(section))
{
throw new ArgumentException("必须指定节点名称", "section");
}
if (string.IsNullOrEmpty(items))
{
throw new ArgumentException("必须指定键值对", "items");
}
return IniOP.WritePrivateProfileSection(section, items, iniFile);
}
/// <summary>
/// 在INI文件中,指定节点写入指定的键及值。如果已经存在,则替换。如果没有则创建。
/// </summary>
/// <param name="iniFile">INI文件</param>
/// <param name="section">节点</param>
/// <param name="key">键</param>
/// <param name="value">值</param>
/// <returns>操作是否成功</returns>
public static bool INIWriteValue(string iniFile, string section, string key, string value)
{
if (string.IsNullOrEmpty(section))
{
throw new ArgumentException("必须指定节点名称", "section");
}
if (string.IsNullOrEmpty(key))
{
throw new ArgumentException("必须指定键名称", "key");
}
if (value == null)
{
throw new ArgumentException("值不能为null", "value");
}
return IniOP.WritePrivateProfileString(section, key, value, iniFile);
}
/// <summary>
/// 在INI文件中,删除指定节点中的指定的键。
/// </summary>
/// <param name="iniFile">INI文件</param>
/// <param name="section">节点</param>
/// <param name="key">键</param>
/// <returns>操作是否成功</returns>
public static bool INIDeleteKey(string iniFile, string section, string key)
{
if (string.IsNullOrEmpty(section))
{
throw new ArgumentException("必须指定节点名称", "section");
}
if (string.IsNullOrEmpty(key))
{
throw new ArgumentException("必须指定键名称", "key");
}
return IniOP.WritePrivateProfileString(section, key, null, iniFile);
}
/// <summary>
/// 在INI文件中,删除指定的节点。
/// </summary>
/// <param name="iniFile">INI文件</param>
/// <param name="section">节点</param>
/// <returns>操作是否成功</returns>
public static bool INIDeleteSection(string iniFile, string section)
{
if (string.IsNullOrEmpty(section))
{
throw new ArgumentException("必须指定节点名称", "section");
}
return IniOP.WritePrivateProfileString(section, null, null, iniFile);
}
/// <summary>
/// 在INI文件中,删除指定节点中的所有内容。
/// </summary>
/// <param name="iniFile">INI文件</param>
/// <param name="section">节点</param>
/// <returns>操作是否成功</returns>
public static bool INIEmptySection(string iniFile, string section)
{
if (string.IsNullOrEmpty(section))
{
throw new ArgumentException("必须指定节点名称", "section");
}
return IniOP.WritePrivateProfileSection(section, string.Empty, iniFile);
}
#endregion
#endregion
}