自定义IniFile操作类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO; namespace Sisley
{
public class INIFileHepler
{ private string Default_IniFileName = "Config.ini"; //INI文件名
private string strIniFilePath; // 返回0表示失败,非0为成功
[DllImport("kernel32", CharSet = CharSet.Auto)]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); // 返回取得字符串缓冲区的长度
[DllImport("kernel32", CharSet = CharSet.Auto)]
private static extern long GetPrivateProfileString(string section, string key, string strDefault, StringBuilder retVal, int size, string filePath);
/*
[DllImport("Kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetPrivateProfileInt(string section, string key, int nDefault, string filePath);
*/ public INIFileHepler()
{
this.strIniFilePath = Directory.GetCurrentDirectory() + "\\" +Default_IniFileName; } public INIFileHepler(string strIniFilePath)
{
if ( Directory.Exists(strIniFilePath))
{
this.strIniFilePath = Directory.GetCurrentDirectory() + "\\" + Default_IniFileName; } this.strIniFilePath = strIniFilePath;
} /// <summary>
/// 写入
/// </summary>
/// <param name="section">写入section</param>
/// <param name="key">写入key</param>
/// <param name="value">写入value</param>
public void WriteIniString(string section, string key, string value)
{
try
{
WritePrivateProfileString(section, key, value, strIniFilePath);
}
catch
{
throw new Exception("配置文件不存在或权限不足导致无法写入");
} } /// <summary>
/// 获取ini配置文件中的字符串
/// </summary>
/// <param name="section">节名</param>
/// <param name="key">键名</param>
/// <param name="strDefault">默认值</param>
/// <param name="retVal">结果缓冲区</param>
/// <param name="size">结果缓冲区大小</param>
/// <returns>返回值</returns>
public string GetIniString(string section, string key, string strDefault, StringBuilder retVal, int size)
{
long liRet = GetPrivateProfileString(section, key, strDefault, retVal, size, strIniFilePath);
//return (liRet >= 1); StringBuilder temp = new StringBuilder();
INIFileHepler.GetPrivateProfileString(section, key, strDefault, temp, , strIniFilePath);
return temp.ToString();
} }
}

使用示例:

设置和设置

  INIFileHepler inifile = new INIFileHepler();
//INIFileHepler inifile = new INIFileHepler(@"D:\MyDocs\CSharp\Program\bin\Debug\TestConfig.ini");
private void button1_Click(object sender, EventArgs e)
{ inifile.WriteIniString("DatabaseConfig", "Server", "192.168.10.3");
inifile.WriteIniString("DatabaseConfig", "DBName", "DB_Live");
inifile.WriteIniString("DatabaseConfig", "UserName", "admin");
inifile.WriteIniString("DatabaseConfig", "UserPassword", "");
} private void button2_Click(object sender, EventArgs e)
{ richTextBox1.AppendText("DatabaseConfig"+"\n");
StringBuilder Server = new StringBuilder();
StringBuilder DBName = new StringBuilder();
StringBuilder UserName = new StringBuilder();
StringBuilder UserPassword = new StringBuilder(); inifile.GetIniString("DatabaseConfig", "Server", "", Server, Server.Capacity); inifile.GetIniString("DatabaseConfig", "DBName", "", DBName, DBName.Capacity);
inifile.GetIniString("DatabaseConfig", "UserName", "", UserName, UserName.Capacity);
inifile.GetIniString("DatabaseConfig", "UserPassword", "", UserPassword, UserPassword.Capacity); richTextBox1.AppendText("Server: " +Server+ "\n");
richTextBox1.AppendText("DBName: " + DBName + "\n");
richTextBox1.AppendText("UserName: " + UserName + "\n");
richTextBox1.AppendText("UserPassword: " + UserPassword + "\n");
}

C# 读取和配置IniFile的更多相关文章

  1. springboot 读取 yml 配置的几种方式

    前言:在springboot 项目中一般默认的配置文件是application.properties,但是实际项目中我们一般会使用application.yml 文件,下面就介绍一下在springbo ...

  2. DOS下读取PCI配置空间信息的汇编程序(通过IOCF8/IOCFC)

    汇编程序编写的读取PCI配置空间信息的代码(通过IOCF8/IOCFC): ;------------------------------------------------ ;功能: 读取PCI 配 ...

  3. Spring Boot 之注解@Component @ConfigurationProperties(prefix = "sms") 使用@ConfigurationProperties读取yml配置

    从spring-boot开始,已经支持yml文件形式的配置,@ConfigurationProperties的大致作用就是通过它可以把properties或者yml配置直接转成对象 @Componen ...

  4. 每日笔记---使用@ConfigurationProperties读取yml配置

    每日笔记---使用@ConfigurationProperties读取yml配置 参考地址  https://www.cnblogs.com/mycs-home/p/8352140.html 1.添加 ...

  5. python 读取consul配置

    自动化通过rcp client调用远端服务接口时,都需要将远端测试服务ip.端口记录在配置文件. 但由于,服务发布或重启会导致ip.端口变动. 以下将通过python-consul 自动去读取cons ...

  6. Springboot(二)-application.yml默认的配置项以及读取自定义配置

    写在前面 ===== spring-boot 版本:2.0.0.RELEASE ===== 读取自定义配置 1.配置文件:sys.properties supply.place=云南 supply.c ...

  7. c++读取lua配置基础类

    一.内容介绍 把lua作为配置文件,里面的参数值的获取,在他人基础上做了修改,并且补充了一维数组的处理方式. 若有不足之处请多多指教. 对于二维数组,没有成功.希望大家继续补充和修改,非常感谢! 二. ...

  8. golang 读取 ini配置信息

      package main //BY: 29295842@qq.com//这个有一定问题   如果配置信息里有中文就不行//[Server] ;MYSQL配置//Server=localhost   ...

  9. 表单配置项写法,表单写成JSON数组套对象,一行是一个数组单位,一列是一个对象单位,然后再写一个公共组件读取这个配置,循环加载slot,外层载入slot的自定义部分,比如input select等,这种写法就是把组件嵌套改为配置方式

    表单配置项写法,表单写成JSON数组套对象,一行是一个数组单位,一列是一个对象单位,然后再写一个公共组件读取这个配置,循环加载slot,外层载入slot的自定义部分,比如input select等,这 ...

随机推荐

  1. 关系型数据之LinQ基本查询

    如下图所示关系型数据,LinQ语句为:

  2. Swift-----类型转换 、 嵌套类型 、 扩展 、 协议 、 访问控制

    使用is和as操作符判断和转换数组中的对象类型 1.1 问题 类型转换可以判断实例的类型,也可以将实例看做是其父类或者子类的实例.在Swift中使用is和as操作符实现类型转换. 本案例定义一个媒体类 ...

  3. python和nodejs的aes128加密对比

    之前的机器人是用python写的,有同事想改写成nodejs版,但是验证一直通不过,于是帮忙爬了一下文档. python版的代码大概是长这样: #encoding:utf8 from Crypto.C ...

  4. http 学习 1-2 chapter2-URL与资源

    URL是因特网资源的标准化名称.URL指向每一条电子信息,告诉他们位于何处,以及如何与之进行交互. URL语法,以及各种URL组件的含义及其所做的工作. 很多Web客户端都支持额URL快捷方式,包括相 ...

  5. c# chart

    1.随便一个例子 string sql = "select distinct count(*) as c,datepart(day,ull_loginTime) as a from user ...

  6. selenium 常见问题整理。

    一:日期控件 selenium不能直接对日期控件操作,可以通过js对日期控件做赋值操作 WebElement inputTimeBox=driver.findElement(by.name(" ...

  7. ipython安装

    sudo pip install jupyter 然后用jupyter notebook来运行

  8. android studio对于错误拼写/不识别的英文单词,给予波浪提示

    使用自己的名字当Tag.却发现有个非常不用好的提示.波浪,我浪个你妹. Typo:In word ‘miyuehu’ less...(Ctrl+F1) spellchecker inspection ...

  9. html页面元素事件丢失

    前段时间做网站包括最近用mvc做oa都发现这个奇怪现象,就是页面加载完毕后页面元素事件(比如按钮点击.复选框change)一切正常,但是当执行查询或者其他操作后页面事件丢失.具体原因暂没有时间分析,现 ...

  10. C的文件操作2

    [转] C语言文件操作  概述 所谓文件(file)一般指存储在外部介质上数据的集合,比如我们经常使用的mp3.mp4.txt.bmp.jpg.exe.rmvb等等.这些文件各有各的用途,我们通常将它 ...