• C#- 操作Ini文件


      以前习惯了使用.NET中的WEB.CONFIG或者APP.CONFIG,最近在做项目的时候遇到了些问题,发现没办法使用这些CONFIG文件。一开始我的做法是建一个文本文件,自己定规律,自己写方法去写新的配置项和读配置项,虽然基本都能实现了,但做的很一般,不够简洁明了。然后我想过用XML来做配置文件,最后又发现INI文件。INI文件已经有别人写好的DLL可以用了,了解多了一些后看到很多软件也用这INI,包括我们的WINDOWS系统也用了很多INI文件,我用了下感觉很不错。

      在网上的了一些文章,再整理了一下下,记录如下,做一个备忘

      

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.InteropServices;
    
    namespace IniFileDemo
    {
        public class IniFile
        {
            public string Path;
    
            public IniFile(string path)
            {
                this.Path = path;
            }
    
            #region 声明读写INI文件的API函数
            [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 defVal, StringBuilder retVal, int size, string filePath);
    
            [DllImport("kernel32")]
            private static extern int GetPrivateProfileString(string section, string key, string defVal, Byte[] retVal, int size, string filePath);
            #endregion
    
            /// <summary>
            /// 写INI文件
            /// </summary>
            /// <param name="section">段落</param>
            /// <param name="key"></param>
            /// <param name="iValue"></param>
            public void IniWriteValue(string section, string key, string iValue)
            {
                WritePrivateProfileString(section, key, iValue, this.Path);
            }
    
            /// <summary>
            /// 读取INI文件
            /// </summary>
            /// <param name="section">段落</param>
            /// <param name="key"></param>
            /// <returns>返回的键值</returns>
            public string IniReadValue(string section, string key)
            {
                StringBuilder temp = new StringBuilder(255);
    
                int i = GetPrivateProfileString(section, key, "", temp, 255, this.Path);
                return temp.ToString();
            }
    
            /// <summary>
            /// 读取INI文件
            /// </summary>
            /// <param name="Section">段,格式[]</param>
            /// <param name="Key"></param>
            /// <returns>返回byte类型的section组或键值组</returns>
            public byte[] IniReadValues(string section, string key)
            {
                byte[] temp = new byte[255];
    
                int i = GetPrivateProfileString(section, key, "", temp, 255, this.Path);
                return temp;
            }
    
        }
    }

    读写DEMO

            private void button1_Click(object sender, EventArgs e)
            {
                IniFile ini = new IniFile(@"d:abc.ini");
               
                //读
                //byte[] sectionByte = ini.IniReadValues("Person", "Love");
                //string result = System.Text.Encoding.ASCII.GetString(sectionByte);
                //MessageBox.Show(result);

                //写
                //ini.IniWriteValue("Person", "Love", "Girl");

            }

  • 相关阅读:
    修改 Mac 默认 PHP 运行环境,给 MAMP 配置全局 Composer
    修改 Mac 默认 PHP 运行环境,给 MAMP 配置全局 Composer
    Intellij Idea/Webstorm/Phpstorm 的高效快捷键
    Intellij Idea/Webstorm/Phpstorm 的高效快捷键
    管理工具推荐
    mongo find
    redis 工具
    Redis Quick Start [熟练版]
    Redis cluster
    redis cluster 使用中出现的问题
  • 原文地址:https://www.cnblogs.com/cxeye/p/3585703.html
Copyright © 2020-2023  润新知