重新封装了一个C#的ini操作类,已经测试过了。需要的自行研究下!
可以读取和写入整型、字符串、boolean以及浮点数的操作。
- using System;
- using System.Text;
- using System.IO;
- using System.Runtime.InteropServices;
- /************************************************
- * https://www.51halcon.com
- * **********************************************/
- namespace soft.51halcon.com
- {
- public class CIniFile
- {
- #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);
- [DllImport("kernel32")]//返回0表示成功,非0为失败
- private static extern int GetPrivateProfileInt(string section, string key, int nDef, string filePath);
- #endregion
- #region 读Ini文件String类型
- public static string ReadIniString(string Section, string Key, string Default, string iniFilePath)
- {
- if (File.Exists(iniFilePath))
- {
- StringBuilder temp = new StringBuilder(1024);
- GetPrivateProfileString(Section, Key, Default, temp, 1024, iniFilePath);
- return temp.ToString();
- }
- else
- {
- return String.Empty;
- }
- }
- #endregion
- #region 写Ini文件String类型
- public static bool WriteIniString(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
- #region 读Ini文件Integer类型
- public static int ReadIniInteger(string Section, string Key,int nDef,string iniFilePath)
- {
- if (File.Exists(iniFilePath))
- {
- StringBuilder temp = new StringBuilder(1024);
- GetPrivateProfileString(Section, Key, nDef.ToString(), temp, 1024, iniFilePath);
- string str = temp.ToString();
- return int.Parse(str);
- }
- else
- {
- return -1;
- }
- }
- #endregion
- #region 写Ini文件Integer类型
- public static bool WriteIniInteger(string Section, string Key, int Val, string iniFilePath)
- {
- if (File.Exists(iniFilePath))
- {
- long OpStation = WritePrivateProfileString(Section, Key, Val.ToString(), iniFilePath);
- if (OpStation == 0)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- else
- {
- return false;
- }
- }
- #endregion
- #region 读Ini文件Double类型
- public static double ReadIniDouble(string Section, string Key, double dDef, string iniFilePath)
- {
- if (File.Exists(iniFilePath))
- {
- StringBuilder temp = new StringBuilder(1024);
- GetPrivateProfileString(Section, Key, dDef.ToString(), temp, 1024, iniFilePath);
- string str = temp.ToString();
- return Convert.ToDouble(str);
- }
- else
- {
- return 0.0;
- }
- }
- #endregion
- #region 写Ini文件Double类型
- public static bool WriteIniDouble(string Section, string Key, double Val, string iniFilePath)
- {
- if (File.Exists(iniFilePath))
- {
- long OpStation = WritePrivateProfileString(Section, Key, Val.ToString(), iniFilePath);
- if (OpStation == 0)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- else
- {
- return false;
- }
- }
- #endregion
- #region 读Ini文件bool类型
- public static bool ReadIniBool(string Section, string Key, bool Val, string iniFilePath)
- {
- if (File.Exists(iniFilePath))
- {
- StringBuilder temp = new StringBuilder(1024);
- GetPrivateProfileString(Section, Key, Val.ToString(), temp, 1024, iniFilePath);
- string str = temp.ToString();
- int nVal = Convert.ToInt32(str);
- return nVal==0?false:true;
- }
- else
- {
- return false;
- }
- }
- #endregion
- #region 写Ini文件bool类型
- public static bool WriteIniBool(string Section, string Key, bool Val, string iniFilePath)
- {
- if (File.Exists(iniFilePath))
- {
- long OpStation = WritePrivateProfileString(Section, Key, Val?"1":"0", iniFilePath);
- if (OpStation == 0)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- else
- {
- return false;
- }
- }
- #endregion
- }
- }
复制代码
工程里面新建项,添加一个新的CIniFile.cs文件即可,复制所有代码到文件中。
1.引用命名空间
2.实现代码
- CIniFile.WriteIniBool("SYSTEM", "Debug", true, strFile);
- CIniFile.WriteIniDouble("SYSTEM", "Double", 7.8, strFile);
复制代码
|