zoukankan      html  css  js  c++  java
  • c# 读写ini文件

    //在 Ini 文件中写数据
    [DllImport("kernel32")]
    private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

    //读取 Ini 文件中的数据
    [DllImport("kernel32")]
    private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

    //读取所有Sections
    [DllImport("kernel32", EntryPoint = "GetPrivateProfileString")]
    private static extern uint GetPrivateProfileStringA(string section, string key, string def, Byte[] retVal, int size, string filePath);

    /// <summary>
    /// 创建Ini文件,并写入数据
    /// </summary>
    /// <param name="section">根节点</param>
    /// <param name="key">键</param>
    /// <param name="val">值</param>
    /// <param name="filePath">路径</param>
    public static void WriteIniData(string section, string key, string val, string filePath)
    {
      if (!File.Exists(filePath))
      {
        var fs = File.Create(filePath);
        fs.Dispose();
        fs.Close();
      }
      WritePrivateProfileString(section, key, val, filePath);
    }

    /// <summary>
    /// 读取 Ini 文件
    /// </summary>
    /// <param name="section">根节点</param>
    /// <param name="key">键</param>
    /// <param name="def">默认数据</param>
    /// <param name="retVal">返回数据</param>
    /// <param name="size">大小</param>
    /// <param name="filePath">路径</param>
    public static void ReadIniData(string section, string key, string def, StringBuilder retVal, int size, string filePath)
    {
      GetPrivateProfileString(section, key, def, retVal, size, filePath);
    }

    public static List<string> ReadAllSection(string filePath)
    {
      List<string> result = new List<string>();
      Byte[] buf = new Byte[65536];
      uint len = GetPrivateProfileStringA(null, null, null, buf, buf.Length, filePath);
      int j = 0;
      for (int i = 0; i < len; i++)
      {
        if (buf[i] == 0)
        {
          result.Add(Encoding.Default.GetString(buf, j, i - j));
          j = i + 1;
        }
      }
      return result;
    }

  • 相关阅读:
    docker 加速器配置目录
    php 超时设置笔记
    php socket通过smtp发送邮件(纯文本、HTML,多收件人,多抄送,多密送)
    fabric 安装
    centos7下使用yum安装pip
    【转】linux tar 压缩
    ASP.NET MVC 5 默认模板的JS和CSS 是怎么加载的?
    NHibernate with ASP.NET MVC 入门示例
    Ajax入门
    NHibernate入门
  • 原文地址:https://www.cnblogs.com/wangye520/p/8184361.html
Copyright © 2011-2022 走看看