设置首页收藏本站
开启左侧

C#实现ini配置文件的读取和写入

[复制链接]
Criss 发表于 2017-10-13 17:28:44 | 显示全部楼层 |阅读模式
重新封装了一个C#的ini操作类,已经测试过了。需要的自行研究下!
可以读取和写入整型、字符串、boolean以及浮点数的操作。
  1. using System;
  2. using System.Text;
  3. using System.IO;
  4. using System.Runtime.InteropServices;
  5. /************************************************
  6. * https://www.51halcon.com
  7. * **********************************************/

  8. namespace soft.51halcon.com
  9. {
  10.     public class CIniFile
  11.     {
  12.         #region API函数声明

  13.         [DllImport("kernel32")]//返回0表示失败,非0为成功
  14.         private static extern long WritePrivateProfileString(string section, string key,string val, string filePath);

  15.         [DllImport("kernel32")]//返回取得字符串缓冲区的长度
  16.         private static extern long GetPrivateProfileString(string section, string key,string def, StringBuilder retVal, int size, string filePath);

  17.         [DllImport("kernel32")]//返回0表示成功,非0为失败
  18.         private static extern int GetPrivateProfileInt(string section, string key, int nDef, string filePath);

  19.         #endregion

  20.         #region 读Ini文件String类型
  21.         public static string ReadIniString(string Section, string Key, string Default, string iniFilePath)
  22.         {
  23.             if (File.Exists(iniFilePath))
  24.             {
  25.                 StringBuilder temp = new StringBuilder(1024);
  26.                 GetPrivateProfileString(Section, Key, Default, temp, 1024, iniFilePath);
  27.                 return temp.ToString();
  28.             }
  29.             else
  30.             {
  31.                 return String.Empty;
  32.             }
  33.         }

  34.         #endregion

  35.         #region 写Ini文件String类型
  36.         public static bool WriteIniString(string Section, string Key, string Value, string iniFilePath)
  37.         {
  38.             if (File.Exists(iniFilePath))
  39.             {
  40.                 long OpStation = WritePrivateProfileString(Section, Key, Value, iniFilePath);
  41.                 if (OpStation == 0)
  42.                 {
  43.                     return false;
  44.                 }
  45.                 else
  46.                 {
  47.                     return true;
  48.                 }
  49.             }
  50.             else
  51.             {
  52.                 return false;
  53.             }
  54.         }

  55.         #endregion

  56.         #region 读Ini文件Integer类型
  57.         public static int ReadIniInteger(string Section, string Key,int nDef,string iniFilePath)
  58.         {
  59.             if (File.Exists(iniFilePath))
  60.             {
  61.                 StringBuilder temp = new StringBuilder(1024);
  62.                 GetPrivateProfileString(Section, Key, nDef.ToString(), temp, 1024, iniFilePath);
  63.                 string str = temp.ToString();
  64.                 return int.Parse(str);
  65.             }
  66.             else
  67.             {
  68.                 return -1;
  69.             }
  70.         }
  71.         #endregion

  72.         #region 写Ini文件Integer类型
  73.         public static bool WriteIniInteger(string Section, string Key, int Val, string iniFilePath)
  74.         {
  75.             if (File.Exists(iniFilePath))
  76.             {
  77.                 long OpStation = WritePrivateProfileString(Section, Key, Val.ToString(), iniFilePath);
  78.                 if (OpStation == 0)
  79.                 {
  80.                     return false;
  81.                 }
  82.                 else
  83.                 {
  84.                     return true;
  85.                 }
  86.             }
  87.             else
  88.             {
  89.                 return false;
  90.             }
  91.         }
  92.         #endregion

  93.         #region 读Ini文件Double类型
  94.         public static double ReadIniDouble(string Section, string Key, double dDef, string iniFilePath)
  95.         {
  96.             if (File.Exists(iniFilePath))
  97.             {
  98.                 StringBuilder temp = new StringBuilder(1024);
  99.                 GetPrivateProfileString(Section, Key, dDef.ToString(), temp, 1024, iniFilePath);
  100.                 string str = temp.ToString();
  101.                 return Convert.ToDouble(str);
  102.             }
  103.             else
  104.             {
  105.                 return 0.0;
  106.             }
  107.         }
  108.         #endregion

  109.         #region 写Ini文件Double类型
  110.         public static bool WriteIniDouble(string Section, string Key, double Val, string iniFilePath)
  111.         {
  112.             if (File.Exists(iniFilePath))
  113.             {
  114.                 long OpStation = WritePrivateProfileString(Section, Key, Val.ToString(), iniFilePath);
  115.                 if (OpStation == 0)
  116.                 {
  117.                     return false;
  118.                 }
  119.                 else
  120.                 {
  121.                     return true;
  122.                 }
  123.             }
  124.             else
  125.             {
  126.                 return false;
  127.             }
  128.         }
  129.         #endregion

  130.         #region 读Ini文件bool类型
  131.         public static bool ReadIniBool(string Section, string Key, bool Val, string iniFilePath)
  132.         {
  133.             if (File.Exists(iniFilePath))
  134.             {
  135.                 StringBuilder temp = new StringBuilder(1024);
  136.                 GetPrivateProfileString(Section, Key, Val.ToString(), temp, 1024, iniFilePath);
  137.                 string str = temp.ToString();
  138.                 int nVal = Convert.ToInt32(str);
  139.                 return nVal==0?false:true;
  140.             }
  141.             else
  142.             {
  143.                 return false;
  144.             }
  145.         }
  146.         #endregion

  147.         #region 写Ini文件bool类型
  148.         public static bool WriteIniBool(string Section, string Key, bool Val, string iniFilePath)
  149.         {
  150.             if (File.Exists(iniFilePath))
  151.             {

  152.                 long OpStation = WritePrivateProfileString(Section, Key, Val?"1":"0", iniFilePath);
  153.                 if (OpStation == 0)
  154.                 {
  155.                     return false;
  156.                 }
  157.                 else
  158.                 {
  159.                     return true;
  160.                 }
  161.             }
  162.             else
  163.             {
  164.                 return false;
  165.             }
  166.         }
  167.         #endregion
  168.     }
  169. }
复制代码

工程里面新建项,添加一个新的CIniFile.cs文件即可,复制所有代码到文件中。

1.引用命名空间
  1. using soft.51halcon.com;
复制代码

2.实现代码
  1. CIniFile.WriteIniBool("SYSTEM", "Debug", true, strFile);
  2. CIniFile.WriteIniDouble("SYSTEM", "Double", 7.8, strFile);
复制代码


奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表