开发者

C#操作INI文件的示例代码

开发者 https://www.devze.com 2023-12-31 10:17 出处:网络 作者: lljss2020
IniFileOp.cs using System; using System.Collepythonctions.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;

IniFileOp.cs

using System;
using System.Collepythonctions.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace xxxxx
{
    class IniFileOp
    {
        //#region 声明读写INI文件的API函数
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(pythonstring section, string key, string val, string filepath);
        //参数说明:section:INI文件中的段落;key:INI文件中的关键字;val:INI文件中关键字的数值;filePath:INI文件的完整的路径和名称。

        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
        //参数说明:section:INI文件中的段落名称;key:INI文件中的关键字;def:无法读取时候时候的缺省数值;retVal:读取数值;size:数值的大小;filePath:INI文件的完整路径和名称
        //#endregion

        public string Path;

        public IniFileOp(string path)
        {
            this.Path = path;
        }


        /// <summary>
        /// 写INI文件
        /// </summary>
        /// <param name="section">段落</param>
        /// <param name="key">键</param>
        /// <param name="iValue">值</param>
        public void IniWriteStr(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 IniReadStr(string section, string keywww.devze.com)
        {
            StringBuilder sb = new StringBuilder(256);

            int i = GetPrivateProfileString(section, key, "", sb, 256, this.Path);
            return sb.ToString();
        }

        /// <summary>
        /// 读取INI文件
        /// </summary>
        /// <param name="Section">段,格式[]</param>
        /// <param name="Key">键</param>
        /// <returns>返回byte类型的section组或键值组</returns>
        public byte[] IniReadBytes(string section, string key)
        {
            StringBuilder sb = new StringBuilder(256);

            int i = GetPrivateProfileString(section, key, "", sb, 256, this.Path);
            string s = sb.ToString();
            byte[] byteArray = System.Text.Encoding.Default.GetBytes(s);
            return byteArray;
        }
    }www.devze.com
}

ConfigPara.ini

[Param]
PauseTime=0

使用

 int pauseTime = 0;

 string path1 = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
 path1 += "ConfigPara.ini";
 IniFileOp inifile = new IniFileOp(path1);

 string str1 = inifile.IniReadStr("Param", "PauseTime");

 if (str1 != "")
 {
     pauseTime = int.Parse(str1);
 }

到此这篇关于C#操作INI文件的示例代码的文章就介绍到这了,更多相关C#操作INI内容请编程搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号