星空网 > 软件开发 > ASP.net

C# 操作ini配置文件

最近使用Winform做一个小系统,由于需要保存一些默认配置项。自然就想到了轻量级的配置文件类型ini。在此也分享和记录一下实现方式,方便以后查询和使用。

废话不多说上代码:

实现公共函数↓

  public static class WinAPI  {    [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 def,    StringBuilder retVal, int size, string filePath);    // 向配置文件写入值    public static void ProfileWriteValue(    string section, string key, string value, string path)    {      WritePrivateProfileString(section, key, value, path);    }    // 读取配置文件的值    public static string ProfileReadValue(    string section, string key, string path)    {      StringBuilder sb = new StringBuilder(255);      GetPrivateProfileString(section, key, "", sb, 255, path);      return sb.ToString().Trim();    }  }

调用实例↓

//配置文件位置string configpath = AppDomain.CurrentDomain.BaseDirectory + "config.ini";//写入配置WinAPI.ProfileWriteValue("Setting", "DefaultSerialPort", ssp.SL_PortName, configpath);//读取配置WinAPI.ProfileReadValue("Setting", "DefaultSerialPort", configpath);

初始化判断是否存在配置,否则创建文件↓

//判断是否存在配置文件
if (!File.Exists(configpath)){ FileStream fs = new FileStream(configpath, FileMode.OpenOrCreate);}

 




原标题:C# 操作ini配置文件

关键词:C#

C#
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

跨境电商英语:https://www.goluckyvip.com/tag/8885.html
方圆泰物流:https://www.goluckyvip.com/tag/88850.html
泰国往国内寄快递:https://www.goluckyvip.com/tag/88851.html
泰国小包快递:https://www.goluckyvip.com/tag/88852.html
往泰国寄东西用什么快递:https://www.goluckyvip.com/tag/88853.html
到泰国国际快递:https://www.goluckyvip.com/tag/88854.html
大福地快捷酒店预订 大福酒店怎么走:https://www.vstour.cn/a/365187.html
三亚有哪些酒店值得入住?:https://www.vstour.cn/a/366173.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流