zoukankan      html  css  js  c++  java
  • C#对INI文件读写

    C#本身没有对INI格式文件的操作类,可以自定义一个IniFile类进行INI文件读写。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text;
    
    namespace MSFramework.Common
    {
        /// <summary>
        /// ini文件类
        /// </summary>
        public class IniFile
        {
            private string m_FileName;
    
            public string FileName
            {
                get { return m_FileName; }
                set { m_FileName = value; }
            }
    
            [DllImport("kernel32.dll")]
            private static extern int GetPrivateProfileInt(
                string lpAppName,
                string lpKeyName,
                int nDefault,
                string lpFileName
                );
    
            [DllImport("kernel32.dll")]
            private static extern int GetPrivateProfileString(
                string lpAppName,
                string lpKeyName,
                string lpDefault,
                StringBuilder lpReturnedString,
                int nSize,
                string lpFileName
                );
    
            [DllImport("kernel32.dll")]
            private static extern int WritePrivateProfileString(
                string lpAppName,
                string lpKeyName,
                string lpString,
                string lpFileName
                );
    
            /// <summary>
            /// 构造函数
            /// </summary>
            /// <param name="aFileName">Ini文件路径</param>
            public IniFile(string aFileName)
            {
                this.m_FileName = aFileName;
            }
    
            /// <summary>
            /// 构造函数
            /// </summary>
            public IniFile()
            { }
    
            /// <summary>
            /// [扩展]读Int数值
            /// </summary>
            /// <param name="section"></param>
            /// <param name="name"></param>
            /// <param name="def">默认值</param>
            /// <returns></returns>
            public int ReadInt(string section, string name, int def)
            {
                return GetPrivateProfileInt(section, name, def, this.m_FileName);
            }
    
            /// <summary>
            /// [扩展]读取string字符串
            /// </summary>
            /// <param name="section"></param>
            /// <param name="name"></param>
            /// <param name="def">默认值</param>
            /// <returns></returns>
            public string ReadString(string section, string name, string def)
            {
                StringBuilder vRetSb = new StringBuilder(2048);
                GetPrivateProfileString(section, name, def, vRetSb, 2048, this.m_FileName);
                return vRetSb.ToString();
            }
    
            /// <summary>
            /// [扩展]写入Int数值,如果不存在 节-键,则会自动创建
            /// </summary>
            /// <param name="section"></param>
            /// <param name="name"></param>
            /// <param name="Ival">写入值</param>
            public void WriteInt(string section, string name, int Ival)
            {
    
                WritePrivateProfileString(section, name, Ival.ToString(), this.m_FileName);
            }
    
            /// <summary>
            /// [扩展]写入String字符串,如果不存在 节-键,则会自动创建
            /// </summary>
            /// <param name="section"></param>
            /// <param name="name"></param>
            /// <param name="strVal">写入值</param>
            public void WriteString(string section, string name, string strVal)
            {
                WritePrivateProfileString(section, name, strVal, this.m_FileName);
            }
    
            /// <summary>
            /// 删除指定的 节
            /// </summary>
            /// <param name="section"></param>
            public void DeleteSection(string section)
            {
                WritePrivateProfileString(section, null, null, this.m_FileName);
            }
    
            /// <summary>
            /// 删除全部 节
            /// </summary>
            public void DeleteAllSection()
            {
                WritePrivateProfileString(null, null, null, this.m_FileName);
            }
    
            /// <summary>
            /// 读取指定 节-键 的值
            /// </summary>
            /// <param name="section"></param>
            /// <param name="name"></param>
            /// <returns></returns>
            public string IniReadValue(string section, string name)
            {
                StringBuilder strSb = new StringBuilder(256);
                GetPrivateProfileString(section, name, "", strSb, 256, this.m_FileName);
                return strSb.ToString();
            }
    
            /// <summary>
            /// 写入指定值,如果不存在 节-键,则会自动创建
            /// </summary>
            /// <param name="section"></param>
            /// <param name="name"></param>
            /// <param name="value"></param>
            public void IniWriteValue(string section, string name, string value)
            {
                WritePrivateProfileString(section, name, value, this.m_FileName);
            }
        }
    }

     应用:

     有原始INI文件:

    执行代码:

                IniFile myIni = new IniFile(Environment.CurrentDirectory + "\Config.ini");
                myIni.WriteInt("Tsection", "tName", 89);
                Console.WriteLine(myIni.ReadString("System", "MainFormName", ""));
                Console.WriteLine(myIni.ReadString("DbServer", "DbFile", ""));
                myIni.WriteString("DbServer", "DbFile", @"E:Work	est2.db");
                Console.WriteLine(myIni.ReadString("DbServer", "DbServer", ""));
                Console.WriteLine(myIni.ReadString("DbServer", "DbFile", ""));

    执行结果:

     

    http://www.cnblogs.com/CUIT-DX037/

  • 相关阅读:
    C语言基础课程 第二课 HelloWorld不为菜鸟所知的秘密
    C语言基础课程 第一课 Linux环境配置小实战httpserver
    Linux企业级开发技术(6)——libevent企业级开发之内存管理
    Linux企业级开发技术(7)——libevent企业级开发之锁和线程
    Linux内存管理学习笔记--概述
    5月16日云栖精选夜读:从0到1构建大数据生态系列1:数据蛮荒中的拓荒之举
    luoguP1063 能量项链
    bzoj1060 [ZJOI2007]时态同步
    bzoj1864 [Zjoi2006]三色二叉树
    bzoj1864 [Zjoi2006]三色二叉树
  • 原文地址:https://www.cnblogs.com/CUIT-DX037/p/6797271.html
Copyright © 2011-2022 走看看