Ini操作类
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text; public class INIFileUtil
{
/// <summary>
/// INI文件地址
/// </summary>
private string path; /// <summary>
/// 初始化
/// </summary>
/// <param name="INIPath">路径</param>
private INIFileUtil(string INIPath)
{
this.path = INIPath;
} public static INIFileUtil LoadIniFile(string iniFile)
{
return new INIFileUtil(iniFile);
}
/// <summary>
/// 构造函数
/// </summary>
private INIFileUtil()
{
} [DllImport("kernel32", EntryPoint = "WritePrivateProfileString")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32", EntryPoint = "GetPrivateProfileString")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
[DllImport("kernel32", EntryPoint = "GetPrivateProfileString")]
private static extern int GetPrivateProfileString(string section, string key, string def, byte[] retVal, int size, string filePath);
[DllImport("kernel32.dll", EntryPoint = "GetPrivateProfileSectionNames", CharSet = CharSet.Ansi)]
private static extern int GetPrivateProfileSectionNames(IntPtr lpszReturnBuffer, int nSize, string filePath);
[DllImport("KERNEL32.DLL ", EntryPoint = "GetPrivateProfileSection", CharSet = CharSet.Ansi)]
private static extern int GetPrivateProfileSection(string lpAppName, byte[] lpReturnedString, int nSize, string filePath); /// <summary>
/// 写INI文件
/// </summary>
/// <param name="Section">分组节点</param>
/// <param name="Key">关键字</param>
/// <param name="Value">值</param>
public void IniWriteValue(string Section, string Key, string Value)
{
INIFileUtil.WritePrivateProfileString(Section, Key, Value, this.path);
} /// <summary>
/// 读取INI文件
/// </summary>
/// <param name="Section">分组节点</param>
/// <param name="Key">关键字</param>
/// <returns></returns>
public string IniReadValue(string Section, string Key)
{
StringBuilder stringBuilder = new StringBuilder();
INIFileUtil.GetPrivateProfileString(Section, Key, "", stringBuilder, , this.path);
return stringBuilder.ToString();
} /// <summary>
/// 读取INI文件
/// </summary>
/// <param name="section">分组节点</param>
/// <param name="key">关键字</param>
/// <returns></returns>
public byte[] IniReadValues(string section, string key)
{
byte[] array = new byte[];
INIFileUtil.GetPrivateProfileString(section, key, "", array, , this.path);
return array;
} /// <summary>
/// 删除ini文件下所有段落
/// </summary>
public void ClearAllSection()
{
this.IniWriteValue(null, null, null);
} /// <summary>
/// 删除ini文件下指定段落下的所有键
/// </summary>
/// <param name="Section">指定段</param>
public void ClearSection(string Section)
{
this.IniWriteValue(Section, null, null);
} /// <summary>
/// 读取一个ini里面所有的节
/// </summary>
/// <param name="sections"></param>
/// <param name="path"></param>
/// <returns></returns>
public int GetAllSectionNames(out List<string> sections)
{
int MAX_BUFFER = ;
IntPtr pReturnedString = Marshal.AllocCoTaskMem(MAX_BUFFER);
int bytesReturned = GetPrivateProfileSectionNames(pReturnedString, MAX_BUFFER, this.path);
if (bytesReturned == )
{
sections = new List<string>();
return -;
}
string local = Marshal.PtrToStringAnsi(pReturnedString, (int)bytesReturned).ToString();
Marshal.FreeCoTaskMem(pReturnedString);
//use of Substring below removes terminating null for split
sections = local.Substring(, local.Length - ).Split('\0').ToList<string>();
return ;
} /// <summary>
/// 得到某个节点下面所有的key和value组合
/// </summary>
/// <param name="section"></param>
/// <param name="keys"></param>
/// <param name="values"></param>
/// <param name="path"></param>
/// <returns></returns>
public int GetAllKeyValues(string section, out string[] keys, out string[] values)
{
byte[] b = new byte[]; GetPrivateProfileSection(section, b, b.Length, this.path);
string s = System.Text.Encoding.Default.GetString(b);
string[] tmp = s.Split((char));
ArrayList result = new ArrayList();
foreach (string r in tmp)
{
if (r != string.Empty)
result.Add(r);
}
keys = new string[result.Count];
values = new string[result.Count];
for (int i = ; i < result.Count; i++)
{
string[] item = result[i].ToString().Split(new char[] { '=' });
if (item.Length == )
{
keys[i] = item[].Trim();
values[i] = item[].Trim();
}
else if (item.Length == )
{
keys[i] = item[].Trim();
values[i] = "";
}
else if (item.Length == )
{
keys[i] = "";
values[i] = "";
}
}
return ;
}
}
Ini操作类的更多相关文章
- 我也分享一个c# ini操作类
刚刚看了一篇 @云菲菲 的关于基于正则的INI辅助类文章:http://www.cnblogs.com/yunfeifei/p/4081977.html,作者写的不错.还看到评论处有一个的地址:htt ...
- C# 配置文件ini操作类
// [ DllImport ( "kernel32" ) ] //private static extern long WritePrivateProfileString ( s ...
- android操作ini工具类
package com.smarteye.common; import java.io.BufferedReader; import java.io.BufferedWriter; import ja ...
- Ini文件操作类
/// <summary> /// Ini文件操作类 /// </summary> public class Ini { // 声明INI文件的写操作函数 WritePriva ...
- C# ini配置文件操作类
/// <summary> /// INI文件操作类 /// </summary> public class IniFileHelper { /// <summary&g ...
- C# 文件操作类大全
C# 文件操作类大全 时间:2015-01-31 16:04:20 阅读:1724 评论:0 收藏:0 [点我收藏+] 标签: 1.创建文件夹 //usin ...
- C# 读取Ini配置文件类
配置文件 为fileName.ini 的文件 第一行必须为空,不然读不出值 [section1] key=value key2=value ......... [section2] key=value ...
- Python 之configparser读取配置操作类
一.为什么要封装 我们为什么要封装,我相信你们在项目开发过程中深有体会,那么这个读取配置工具类,又是为了什么? 为了项目参数配置的灵活性,不要改动到源码 为了信息的安全(一定层面的),体现代码重用性 ...
- 一个数据库操作类,适用于Oracle,ACCESS,SQLSERVER
最近做了一个数据诊断的项目,里面自己写了一个数据库的操作类,包含:连接数据库.读数据表.执行SQL操作,释放数据库等组成,希望对大家有用,由于水平有限,若有错误或者代码不足地方欢迎指正,谢谢. ADO ...
随机推荐
- SpringKafka生产端配置类ProducerConfig.java源码
/** * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreeme ...
- tomcat架构分析(容器类)
Tomcat提供了engine,host,context及wrapper四种容器.在总体结构中已经阐述了他们之间的包含关系.这四种容器继承了一个容器基类,因此可以定制化.当然,tomcat也提供了标准 ...
- java web 应用中包,接口的设计
采用标准的架构:描述从低层到高层首先是系统分析,找出你需要什么功能,然后按照下面的步骤执行: 数据库层:数据库层就是SQL语句.数据库.表.视图.触发器等等的创建和管理.这一层和JAVA无关,但是却是 ...
- 【WPF/C#】使用BackgroundWorker实现多线程/异步操作
做WPF时需要做一个异步加载时的Loading遮罩,搜Stackoverflow看到很多方法,看到了这个插件: BusyIndicator in the extended WPF Toolkit 同时 ...
- Linux while 获取键盘输入退出
c 语言实现如下: #include <stdio.h> #include <stdlib.h> #include <string.h> #include < ...
- Ajax-ajax实例4-多级联动菜单
项目结构: 项目运行: 技术要点: 1.4.1 技术要点在分析具体的实现代码之前,先介绍一下本例的几个技术要点.1 .选项的动态创建与删除document 对象的 createElement 方法可以 ...
- No supported encrypter found. The cipher and / or key length are invalid.
终端使用如下命令: php artisan key:generate 将生成的key复制到config/app.php替换82行的APP_KEY键值.
- .NET操作Excel笔记
如果你新建一个项目的话,首先要添加Microsoft.Office.Core 与Microsoft.Office.Interop.Exce这两个应用,然后就能很方便的操作了,示例代码(只实现了简单的读 ...
- Linux Tcl和Expect的安装
一.先安装Tcl 1.下载:tcl版本 8.4.19 http://sourceforge.net/projects/tcl/files/Tcl/8.4.19/tcl8.4.19-src.tar.gz ...
- Windoows窗口程序二
WNDCLASS属性style取值: CS_GLOBALCLASS--应用程序全局窗口类 CS_BYTEALIGNCLIENT--窗口客户区的水平位置8倍数对齐 CS_BYTEALIGNWINDOW- ...