zoukankan      html  css  js  c++  java
  • 【转】ini载入保存类,操作INI配置文件方便的很

    /******************************************************************
     * 
     *  ^_^ 恶猫 独门商标 挖哈哈
     * 
     *  QQ:>23559055 
     * 
     *  Site:>http://emao.me  
     * 
     * ================================================================
     * 
     * 说明:
     * 
     *        1.命名空间: Emao_CfgManager
     *          类名    : CfgManager
     * 
     *         2.声明: CfgManager cm = new CfgManager();
     * 
     *        3.具体方法自己看代码吧哈。简单的很。
     *                
     *                  分别是:add() readAll() writeAll()
     * 
     ******************************************************************/
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Collections;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    
    namespace Emao_CfgManager
    {
        public class CfgManager
        {
            private Dictionary<string ,string> dict= new Dictionary<string,string>();
            private string filename = Application.StartupPath + @"config.ini";
    
            #region API
            [DllImport("kernel32.dll")]
            public extern static int GetPrivateProfileString(string segName, string keyName, string sDefault, byte[] bt, int nSize, string fileName);
            [DllImport("kernel32.dll")]
            public extern static int WritePrivateProfileString(string segName, string keyName, string sValue, string fileName);
    
            [DllImport("kernel32.dll")]
            public extern static int GetPrivateProfileSection(string segName, StringBuilder buffer, int nSize, string fileName);
            [DllImport("kernel32.dll")]
            public extern static int WritePrivateProfileSection(string segName, string sValue, string fileName);
    
    
            #endregion
    
            #region constructure
            public CfgManager()
            {
                ReadAll();
            }
            public CfgManager(string inipath)
            {
                filename = inipath;
                ReadAll();
            }
            #endregion
    
            #region method
            public int Add(object key, object value)
            {
                if (this.dict.ContainsKey(key.ToString()))
                    this.dict[key.ToString()] = value.ToString();
                else
                    this.dict.Add(key.ToString(), value.ToString());
                return this.dict.Count;
            }
            public void ReadAll()
            {
                this.dict.Clear();
                //读取ini文件所有
                StringBuilder sb = new StringBuilder(25500);
                Byte[] bt = new Byte[25000];
                int len = GetPrivateProfileString("Config", null, "", bt, bt.GetUpperBound(0), filename);
                if (len != 0)
                {
                    int start = 0;
                    for (int i = 0; i < len; i++)
                    {
                        if ((bt[i] == 0) && ((i - start) > 0))
                        {
                            String s = Encoding.GetEncoding(0).GetString(bt, start, i - start);
                            Byte[] temp = new Byte[25500];
                            int i2 = GetPrivateProfileString("Config", s, "", temp, 25500, filename);
                            string value = System.Text.Encoding.Default.GetString(temp).Replace('', ' ');
                            this.dict.Add(s, value.TrimEnd());
                            start = i + 1;
                        }
                    }
                }
            }
            public void ReadAll(string sectionName)
            {
                this.dict.Clear();
                //读取指定节点
            }
            public void WriteAll()
            {
                foreach (var dic in dict)
                {
                    WritePrivateProfileString("Config",dic.Key,dic.Value,filename);
                }
            }
            public string this[string key]
            {
                get
                {
                    if (key.Trim() == "")
                    {
                        return "";
                    }
                    if (!this.dict.ContainsKey(key)) return "";
                    return this.dict[key];
                }
                set
                {
                    if (!this.dict.ContainsKey(key))
                        this.dict.Add(key, value);
                    else
                        this.dict[key] = value;
                }
            }
            #endregion
    
            #region Function
            
            #endregion
    
            #region property
            public int Count
            {
                get { return this.dict.Count; }
            }
            #endregion
    
        }
    }


    原文:http://www.oschina.net/code/snippet_191468_13277

  • 相关阅读:
    基于C#的Cs架构简单开发一花店的售卖系统4
    基于C#的Cs架构简单开发一花店的售卖系统3
    基于C#的Cs架构简单开发一花店的售卖系统2
    基于C#的Cs架构简单开发一花店的售卖系统1
    经典阅读-构建之法
    自动执行后端方法的界面设计方式
    jsp页面采用超链接标签传值方式及注意事项
    经典阅读-构建之法
    基于springboot+mybatis+echarts实现数据可视化
    eclipse 搭建hadoop开发环境,并连接虚拟机hadoop-2.5.2,运行wordcount
  • 原文地址:https://www.cnblogs.com/anduinlothar/p/3523396.html
Copyright © 2011-2022 走看看