📄 controlini.cs
字号:
// 目的: 存储与读取*.ini文件(函数)
// 存储和读取非值以及字符串,存储/读取非值:WriteIniTF/GetIniTF
// 存储/读取字符串:WriteIniStr/GetIniStr'
// 输入: 指定节点名,指定主键
// WriteIniTF/GetIniTF: 节点名、主键、键值/(省略)、文件名
// WriteIniStr/GetIniStr: 节点名、主键、键值/(省略)、文件名
// AppName: 节点名(默认为Setting)
// AppPath: 路径名
// In_Key: 键名(标示)
// NodeName:键值(输入的数据)
// 返回: 返回指定节点名主键的键值
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using System.Windows.Forms;
namespace SoIniEdit
{
public class ControlIni
{
//引用API
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string defVal, StringBuilder retVal, int size, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string defVal, Byte[] retVal, int size, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileSection(string section, string key, int size, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivatePfileInt(string section, string key, int val, string filePath);
public static string strSub; //程序过程函数名
public static string strCls="ControlIni"; //代码文件名
public static string strErrs; //运行说明
public static Boolean bShowMessageBox = false; //是否弹出错误提示框(默认不弹出)
public static string AppPath; //路径名
public static string AppName; //节点名
//获取路径 当nRoot为0 时表示系统路径、1表示用户传入ini文件完整路径 下面函数中nRoot同样的意思
public static String GetPath(string PathName, int nRoot)
{
try
{
strSub = "GetPath";
//按输入条件重构INI文件路径
if (nRoot == 0)
{
//程序根目录路径
AppPath = Directory.GetCurrentDirectory() + "\\" + PathName + ".ini";
}
else
{
//自定义路径
AppPath = PathName;
}
return AppPath;
}
catch (Exception Err)
{
strErrs = Err.Message;
strErrs = "获取路径失败!";
SubError("", "", PathName, strErrs );
return Err.Message; //返回异常消息
}
finally
{
//程序运行日志
SubLog(strSub);
}
}
public static String GetPath(string PathName)
{
strSub = "GetPath2";
//默认加载为系统根目录路径
int nRoot = 0;
if (nRoot == 0)
{
//程序根目录路径
AppPath = Directory.GetCurrentDirectory() + "\\" + PathName + ".ini";
}
else
{
//自定义路径
AppPath = PathName;
}
return AppPath;
}
//获取节点
public static string Node(string NodeName)
{
try
{
strSub = "Node";
if (NodeName == null)
{
AppName = "Setting"; //如果节点名为空,设置为默认 Setting
}
else
{
AppName = NodeName; //获得节点名称
}
return AppName;
}
catch(Exception e)
{
strErrs = "读取Bool型键值失败!";
SubError(NodeName , "", "", strErrs );
return e.Message;
}
finally
{
//程序运行日志
SubLog(strSub);
}
}
//====================================函数===========================================
//目的: 存储/读取非值
//假设: Setting: 节点名; AppPath: 路径
//输入:
// AppName: 节点名
// In_Key: 主键
// In_Data: 键值
// AppPath: 路径名
// 返回: False/True
//===============================================================================
//读取Bool 型
public static bool GetIniTF(string NodeName, string In_Key, string PathName,int nRoot)
{
strSub = "GetIniTF";
try
{
GetPath(PathName, nRoot); //获取路径
Node(NodeName); //获取节点
StringBuilder temp = new StringBuilder(255);
//调用API,获取节点值
GetPrivateProfileString(NodeName, In_Key, "", temp, 255, AppPath);
if (temp.ToString() == "1") return true; //如果是1返回true
else return false;
}
catch
{
strErrs = "读取Bool型键值失败!";
SubError(NodeName , In_Key, PathName , strErrs ); //调用异常函数
return false ;
}
finally
{
//程序运行日志
SubLog(strSub);
}
}
//写入Bool型
public static bool WriteInitTF(string NodeName, string In_Key, bool In_Data, string PathName,int nRoot)
{
strSub = "WriteInitTF";
try
{
GetPath(PathName, nRoot);//获取路径
Node(NodeName); //获取节点
if (In_Data == true) //如果键值为真 写入1
{
//调用API,根据键值类型写入值
WritePrivateProfileString(NodeName, In_Key, "1", AppPath);
return true;
}
else //如果键值为假 写入0
{
//调用API,根据键值类型写入值
WritePrivateProfileString(NodeName, In_Key, "0", AppPath);
return false;
}
}
catch
{
strErrs = "写入Bool型键值失败!";
SubError(NodeName, In_Key, PathName, strErrs);
return false;
}
finally
{
//程序运行日志
SubLog(strSub);
}
}
// ====================================函数===================================
//目的: 存储/读取字符串值
//假设: AppName: 节点名; AppPath: 路径(可以为空)
//输入:
//AppName: 节点名
//In_Key: 主键
//In_Data: 键值
//AppPath: 路径名
//返回: 指定节点的主键键值
//==================================================================================
//读取字符型
public static string GetIniStr(string NodeName, string In_Key, string PathName,int nRoot)
{
strSub = "GetIniStr";
try
{
GetPath(PathName, nRoot);//获取路径
Node(NodeName); //获取节点
StringBuilder temp = new StringBuilder(255);
//如果节点、键名 、路径为空,返回
if ((NodeName == null) || (In_Key == null) || (PathName == null))
{
return null;
}
//调用API,获取字符型键值
int i = GetPrivateProfileString(NodeName, In_Key, "", temp, 255, AppPath);
return temp.ToString();
}
catch (Exception e)
{
strErrs = "读取字符型键值失败!";
SubError(NodeName , In_Key , PathName, strErrs );
return e.Message;
}
finally
{
//程序运行日志
SubLog(strSub);
}
}
//写入字符型
public static bool WriteIniStr(string NodeName, string In_Key, string In_Data, string PathName,int nRoot)
{
strSub = "WriteIniStr";
try
{
GetPath(PathName, nRoot);//获取路径
Node(NodeName); //获取节点
//如果节点、键名、路径为空,返回
if ((NodeName == null) || (In_Key == null) || (PathName == null))
{
return false;
}
else
{
//调用API写入字符型键值
WritePrivateProfileString(NodeName, In_Key, In_Data, AppPath);
return true;
}
}
catch
{
strErrs = "写入字符型键值失败!";
SubError(NodeName , In_Key , PathName ,strErrs);
return false ;
}
finally
{
//程序运行日志
SubLog(strSub);
}
}
//删除指定节点
public static bool DelNode(string NodeName, string PathName,int nRoot)
{
strSub = "DelNode";
try
{
GetPath(PathName, nRoot);//获取路径
Node(NodeName); //获取节点
//如果节点、键 、路径为空,返回
WritePrivateProfileString(AppName, "", "", AppPath);
return true;
}
catch
{
strErrs = "删除指定节点失败!";
SubError(NodeName, "", PathName, strErrs);
return false;
}
finally
{
//程序运行日志
SubLog(strSub);
}
}
//删除指定键名
public static bool DelKey(string NodeName, string In_Key, string PathName,int nRoot)
{
strSub = "DelKey";
try
{
GetPath(PathName, nRoot); //获取路径
Node(NodeName); //获取节点
//如果节点、键 、路径为空,返回
WritePrivateProfileString(AppName, In_Key, "", AppPath);
return true;
}
catch
{
strErrs = "删除指定键名失败!";
SubError(NodeName, In_Key, PathName, strErrs );
return false;
}
finally
{
//程序运行日志
SubLog(strSub );
}
}
//捕捉错误信息
public static void SubError(string NodeName, string In_Key, string PathName, string strErrs)
{
String strErr;
strErr = strErrs + "([" + NodeName + "][" + In_Key + "]{" + PathName + "}.)";
if (bShowMessageBox==false)
{
}
else
{
MessageBox.Show(strErr);
}
//程序运行异常日志
SubErrLog(strSub );
}
//记录系统日志
public static void SubLog(string strSubName)
{
//调用外部接口函数写函数运行过程详细情况(预留)
}
public static void SubErrLog(string strSubName)
{
//写函数运行过程异常详细情况(预留)
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -