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

    通过调用win32 api可以直接读写ini文件

    写了一个助手类方便操作:

    using System;
    using System.Text;
    using System.Runtime.InteropServices;
    
    public static class IniFileHelper
    {
        [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 Write(string section, string key, string val, string filePath)
        {
            WritePrivateProfileString(section, key, val, filePath);
        }
    
        public static string Read(string section, string key, string filePath)
        {
            var stringBuilder = new StringBuilder(255);
            GetPrivateProfileString(section, key, string.Empty, stringBuilder, 255, filePath);
            return stringBuilder.ToString();
        }
    }
    IniFileHelper

    测试代码:

    static void Main(string[] args)
    {
        IniFileHelper.Write("BaseSetting", "Language", "en_us", "./Config.ini");
        IniFileHelper.Write("Sound", "Enable", "True", "./Config.ini");
        IniFileHelper.Write("Sound", "Volume", "100", "./Config.ini");
    }

    运行结果:

    读取测试:

  • 相关阅读:
    MJRefreshFooterView
    UIActionSheet
    UIAlertView带textField
    SIAlertView
    旋转 锚点
    centos7.2 Apache+PHP7.2+Mysql5.6环境搭建
    ubuntu16.04 mysql 开启远程连接
    Ubuntu16.04重新安装MySQL数据库
    Ubuntu16.04彻底卸载MySQL
    laravel框架基础(2)---laravel项目加载机制
  • 原文地址:https://www.cnblogs.com/hont/p/5151984.html
Copyright © 2011-2022 走看看