ini配置文件读取类 C#

datart 二开环境搭建 下载源码, 地址: https://github.com/running-elephant/datart, dev 分支; 解压后使用 idea 打开; 服务端: 3.1 安装依赖, idea会自动安装; 3.2 修改配置文件 /config/application.yml 3.3 创建数据库,并将bin/datart.sql导入到数据库中 3.4 入口: /server/src/main/java/datart/DatartServerApplication.java [外链图片转存失败,源站可能有防. 阅读详情

using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Collections;
using System.Collections.Specialized;

public class IniFiles
{
    public string FileName; //INI文件名
    //声明读写INI文件的API函数
    [DllImport("kernel32")]
    private static extern bool WritePrivateProfileString(string section, string key, string val, string filePath);
    [DllImport("kernel32")]
    private static extern int GetPrivateProfileString(string section, string key, string def, byte[] retVal, int size, string filePath);
    //类的构造函数,传递INI文件名
    public IniFiles(string AFileName)
    {
        // 判断文件是否存在
        FileInfo fileInfo = new FileInfo(AFileName);
        //Todo:搞清枚举的用法
        if ((!fileInfo.Exists))
        { //|| (FileAttributes.Directory in fileInfo.Attributes))
            //文件不存在,建立文件
            System.IO.StreamWriter sw = new System.IO.StreamWriter(AFileName, false, System.Text.Encoding.Default);
            try
            {
                sw.Write("#表格配置档案");
                sw.Close();
            }

            catch
            {
                throw (new ApplicationException("Ini文件不存在"));
            }
        }
        //必须是完全路径,不能是相对路径
        FileName = fileInfo.FullName;
    }
    //写INI文件
    public void WriteString(string Section, string Ident, string Value)
    {
        if (!WritePrivateProfileString(Section, Ident, Value, FileName))
        {

            throw (new ApplicationException("写Ini文件出错"));
        }
    }
    //读取INI文件指定
    public string ReadString(string Section, string Ident, string Default)
    {
        Byte[] Buffer = new Byte[65535];
        int bufLen = GetPrivateProfileString(Section, Ident, Default, Buffer, Buffer.GetUpperBound(0), FileName);
        //必须设定0(系统默认的代码页)的编码方式,否则无法支持中文
        string s = Encoding.GetEncoding(0).GetString(Buffer);
        s = s.Substring(0, bufLen);
        return s.Trim();
    }

    //读整数
    public int ReadInteger(string Section, string Ident, int Default)
    {
        string intStr = ReadString(Section, Ident, Convert.ToString(Default));
        try
        {
            return Convert.ToInt32(intStr);

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            return Default;
        }
    }

    //写整数
    public void WriteInteger(string Section, string Ident, int Value)
    {
        WriteString(Section, Ident, Value.ToString());
    }

    //读布尔
    public bool ReadBool(string Section, string Ident, bool Default)
    {
        try
        {
            return Convert.ToBoolean(ReadString(Section, Ident, Convert.ToString(Default)));
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            return Default;
        }
    }

    //写Bool
    public void WriteBool(string Section, string Ident, bool Value)
    {
        WriteString(Section, Ident, Convert.ToString(Value));
    }

    //从Ini文件中,将指定的Section名称中的所有Ident添加到列表中
    public void ReadSection(string Section, StringCollection Idents)
    {
        Byte[] Buffer = new Byte[16384];
        //Idents.Clear();

        int bufLen = GetPrivateProfileString(Section, null, null, Buffer, Buffer.GetUpperBound(0),
         FileName);
        //对Section进行解析
        GetStringsFromBuffer(Buffer, bufLen, Idents);
    }

    private void GetStringsFromBuffer(Byte[] Buffer, int bufLen, StringCollection Strings)
    {
        Strings.Clear();
        if (bufLen != 0)
        {
            int start = 0;
            for (int i = 0; i < bufLen; i++)
            {
                if ((Buffer[i] == 0) && ((i - start) > 0))
                {
                    String s = Encoding.GetEncoding(0).GetString(Buffer, start, i - start);
                    Strings.Add(s);
                    start = i + 1;
                }
            }
        }
    }
    //从Ini文件中,读取所有的Sections的名称
    public void ReadSections(StringCollection SectionList)
    {
        //Note:必须得用Bytes来实现,StringBuilder只能取到第一个Section
        byte[] Buffer = new byte[65535];
        int bufLen = 0;
        bufLen = GetPrivateProfileString(null, null, null, Buffer,
         Buffer.GetUpperBound(0), FileName);
        GetStringsFromBuffer(Buffer, bufLen, SectionList);
    }
    //读取指定的Section的所有Value到列表中
    public void ReadSectionValues(string Section, NameValueCollection Values)
    {
        StringCollection KeyList = new StringCollection();
        ReadSection(Section, KeyList);
        Values.Clear();
        foreach (string key in KeyList)
        {
            Values.Add(key, ReadString(Section, key, ""));

        }
    }
    /**/
    ////读取指定的Section的所有Value到列表中,
    //public void ReadSectionValues(string Section, NameValueCollection Values,char splitString)
    //{  string sectionValue;
    //  string[] sectionValueSplit;
    //  StringCollection KeyList = new StringCollection();
    //  ReadSection(Section, KeyList);
    //  Values.Clear();
    //  foreach (string key in KeyList)
    //  {
    //    sectionValue=ReadString(Section, key, "");
    //    sectionValueSplit=sectionValue.Split(splitString);
    //    Values.Add(key, sectionValueSplit[0].ToString(),sectionValueSplit[1].ToString());

    //  }
    //}
    //清除某个Section
    public void EraseSection(string Section)
    {
        //
        if (!WritePrivateProfileString(Section, null, null, FileName))
        {

            throw (new ApplicationException("无法清除Ini文件中的Section"));
        }
    }
    //删除某个Section下的键
    public void DeleteKey(string Section, string Ident)
    {
        WritePrivateProfileString(Section, Ident, null, FileName);
    }
    //Note:对于Win9X,来说需要实现UpdateFile方法将缓冲中的数据写入文件
    //在Win NT, 2000和XP上,都是直接写文件,没有缓冲,所以,无须实现UpdateFile
    //执行完对Ini文件的修改之后,应该调用本方法更新缓冲区。
    public void UpdateFile()
    {
        WritePrivateProfileString(null, null, null, FileName);
    }

    //检查某个Section下的某个键值是否存在
    public bool ValueExists(string Section, string Ident)
    {
        //
        StringCollection Idents = new StringCollection();
        ReadSection(Section, Idents);
        return Idents.IndexOf(Ident) > -1;
    }

    //确保资源的释放
    ~IniFiles()
    {
        UpdateFile();
    }
}

 

Android 中 app freezer 原理详解(二):S 版本 在之前的两篇博文《Android 中app内存回收优化(一)》和《Android 中app内存回收优化(二)》中详细剖析了 Android 中 app 内存优化的流程。这个机制的管理通过 CachedAppOptimizer 类管理,为什么叫这个名字,而不叫 AppCompact 等?freezer,一个针对应用进程长期处于 Cached 状态的优化。在之前博文《app freezer 原理 R 版本》 阅读详情

相关推荐

C#调用Halcon程序三种方法

C#调用Halcon程序三种方法 1.halcon导出C#文件,在C#里面调用 2.在C#里面使用Halcon引擎调用Halcon程序 3.在C#里面使用Halcon引擎调用.hdev下本地函数或者.hdvp外部函数 Halcon引擎手册:C:/Program Files/MVTec/HALCON-21.11-Progress/doc_en_US/html/manuals/programmers_guide/programmers_guide_0120.html 1.导出C#(代码我只给出类,需要调用)

A541925504的博客 1万+

[引用]关于C#操作INI文件的总结

在写项目时遇到C#对ini文件的操作,在网上找了一下,看到不错就顺便收集了一下:INI文件其实是一种具有特定结构的文本文件,它的构成分为三部分,结构如下: [Section1]key1=value2key1=value2……[Section2]key1=value1key2=value2…… 文件由若干个段落(section)组成,每个段落又分成若干个键(key)和值...

weixin_34349320的博客 1200

BCIC IV 2a数据集实战:从MATLAB导入到EEG信号分析全流程

本文提供了BCIC IV 2a数据集从MATLAB导入到EEG信号分析的完整实战流程。详细解析了数据集结构,区分了EEG与EOG信号,并演示了信号可视化、基础预处理(滤波、分段、基线校正)等关键步骤,帮助脑机接口研究者快速上手处理这一经典运动想象数据集。

weixin_29047919的博客 468

btye[] b = new byte[1024]

btye[] b = new byte[1024];   1024是作为缓冲区的大小。作用就是:作为缓冲区大小的作用,会影响导出时占用内存大小。    即说你先定义了一个byte类型的数组,数组长度为1024。也就是说你最多可以存1024个字节的东西,如果超过这个值就会报溢出的异常了    1024b=1k 一般我们定义缓冲区基本上是1024的整数倍       InputSt

origination_star的博客 5416

2026年软件测试职业院校技能大赛—ERP管理平台-采购入库模块Bug定位与查找

竞赛对接软件测试相关标准,以“ERP管理平台” 为被测系统,围绕软件测试领域的主流技术及工具,考查选手面 对实际问题的综合分析能力,对测试文档、测试用例的设计能力, 对单元测试、自动化测试、性能测试、接口测试等测试工具及方 法的掌握程度,检验选手团队协作能力、组织规划能力、方法运用能力等。

我是网络安全兼技能大赛工程师,专注网络安全技术学习与技能大赛实战!持续分享最新的技术文章,帮你少走弯路,一起在技术赛道上进步~ 194

Apifox接口测试+自动化测试

Apifox 接口测试+自动化测试快速入门!

程序员小杨哥的博客 1万+

C#一个Ini操作类

注:原创作者我不记得了,反正不是我,我原来是搞delphi的,编写这个代码的作者也是写delphi,所以我喜欢用这个类,和delphi提供的TIniFile一样的用法。以下是源码,带全部注解。 using System; using System.IO; using System.Runtime.InteropServices; using Sy...

weixin_34273481的博客 168

2025年全国青少年信息素养大赛初赛真题(算法创意实践挑战赛C++初中组:文末附答案)

2025年全国青少年信息素养大赛初赛真题(算法创意实践挑战赛C++初中组:文末附答案)

王老师青少年编程 3395

c#读写ini文件

INI所包含的最基本的“元素”就是参数(parameter),每个参数都有一个name和一个value,name和value由等号“=”隔开,name在等号的左边。ini配置文件由节、键、值组成。

apple12138的博客 1652

MIKE水动力笔记8_冷启动与热启动

在【MIKE水动力笔记5_建立水动力模型】中,讲到【Step 2 设置模型参数】中的【Initial Conditions】时,提到了冷启动与热启动。本博文就详细讲解何为冷启动、热启动,并通过一个案例具体分析两者不同。...

晏长街的博客 6568

C# 读取ini配置文件

C# 读取ini配置文件

heros_bag的博客 1657

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

C# 读取Ini配置文件类

配置文件 为fileName.ini 的文件 第一行必须为空,不然读不出值[section1] key=value key2=value ......... [section2] key=value key2=value ......... 代码如下: using System; using System.Runtime.InteropServices; ...

weixin_33737134的博客 177

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

C#读取Ini文件

废话不多说,直接上代码 1:引入命名空间 System.Runtime.InteropServices System.IO 2:写一个类 iniHelper public class IniHelper { [DllImport("kernel32")]//返回0表示失败,非0为成功 private static extern long WritePrivateProfileString(string section, string key, string val

qq_41543009的博客 1678

C#之配置文件Ini

1、IniConfig类 class IniConfig { public string path="./log.ini"; //INI文件名 //声明写INI文件的API函数 [DllImport("kernel32", CharSet = CharSet.Unicode)] private static extern long WritePrivateProfileString(string sect

qq_41672428的博客 1016

C# 读取配置文件

【代码】C# 读取配置文件。

weixin_43250974的博客 415

C#几种读取文件的方式

//通过File读取文件的所有行的内容 string[] infile = File.ReadAllLines("AppConfig.ini", Encoding.GetEncoding("GBK")); for(int i = 0; i < infile.Length; i++) { ...

baidu_38995168的博客 3547

INI 文件读取专用类

一、INI文件读取专用public class IniFile{    ///      /// 读取INI文件专用类     ///      private Hashtable iniFile = new Hashtable();    public int Count { get { return iniFile.Count; } }    pub

xlfancy的专栏 2287

C#读取ini配置文件

在使用C#与数据库进行连接的过程中感觉修改代码来改变连接字符串不甚方便,尤其是在可移植性方面很不好,总不能在让用户对代码进行编辑吧。所以搞一个配置文件就可以动态读取配置文件中的配置信息了,不用在对代码进行修改、重新编译。 ini配置文件 ini配置文件由许多section组成,每个section下又包含众多属性,例如 (有一点必须要重点强调一下:配置文件开始一定要空一个空行,不然无法读取成功!!!...

日月 1104

C# 关于 ini 配置文件的操作

Ini 配置文件操作 在C#语言中,VS平台并没有给大家提供一个专门用来操作INI配置文件的类。 在本文中主要编写了一个特定的类用来对Ini文件进行操作。 代码: /// /// IniFiles类 /// public class IniFiles { //声明读写INI文件的API函数 [DllImpor

趁着年轻去旅行 890

python读取ini_python读取ini文件

原博文2019-09-06 07:59 −ini 文件名 base.ini ini 创建 file--new--File 命名 base.ini ini 文件内容格式 . [appbase] platformName=Android platformVersion=6.0.1 devic...02026相关推荐2019-12-20 16:16 −安装pip install confi...

weixin_39532352的博客 119

C# INI配置文件读写

using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.IO; class IniConfig { private string inipath = AppDomain.C...

fanwenhu的专栏 518

c# ini(配置文件)文件读写(简单类)

//本文列出了ini类,不考虑节(section),用于读写配置文件。 //在ini类的后面演示了如何使用该类。

weixin_39926429的博客 1163

INI配置文件帮助类

【代码】INI配置文件帮助类。

MOYAN122的博客 282

c# 如何读取和写入ini(配置文件)

/// <summary> /// 配置文件操作类 /// </summary> public class iniOperate { /// <summary> /// ini配置文件路径 /// </summary> public string inipath;...

weixin_34209851的博客 347

c#读取ini文件中内容_在C#中读取和解析INI文件

c#读取ini文件中内容 此类利用System.Collections.Hashtable枚举INI文件中的所有设置,以便于访问。 它非常简单,并且完全可重用。 对于需要设置文件的任何应用程序来说都是可靠的补充。 usingSystem; usingSystem.IO; usingSystem.Collections; publicclassIniParser { priv...

IT与开发人员的地盘 1001
上一篇: 数据库全文搜索批量替换字符串
下一篇: C#类的修饰符
flon
博客等级 码龄22年 22粉丝 46原创
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值