博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Ini操作类
阅读量:6262 次
发布时间:2019-06-22

本文共 4656 字,大约阅读时间需要 15 分钟。

using System;using System.Collections;using System.Collections.Generic;using System.Linq;using System.Runtime.InteropServices;using System.Text;public class INIFileUtil{    ///     /// INI文件地址    ///     private string path;    ///     /// 初始化    ///     /// 路径    private INIFileUtil(string INIPath)    {        this.path = INIPath;    }    public static INIFileUtil LoadIniFile(string iniFile)    {        return new INIFileUtil(iniFile);    }    ///     /// 构造函数    ///     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);    ///     /// 写INI文件    ///     /// 分组节点    /// 关键字    /// 值    public void IniWriteValue(string Section, string Key, string Value)    {        INIFileUtil.WritePrivateProfileString(Section, Key, Value, this.path);    }    ///     ///  读取INI文件    ///     /// 分组节点    /// 关键字    /// 
public string IniReadValue(string Section, string Key) { StringBuilder stringBuilder = new StringBuilder(255); INIFileUtil.GetPrivateProfileString(Section, Key, "", stringBuilder, 255, this.path); return stringBuilder.ToString(); } /// /// 读取INI文件 /// /// 分组节点 /// 关键字 ///
public byte[] IniReadValues(string section, string key) { byte[] array = new byte[255]; INIFileUtil.GetPrivateProfileString(section, key, "", array, 255, this.path); return array; } /// /// 删除ini文件下所有段落 /// public void ClearAllSection() { this.IniWriteValue(null, null, null); } /// /// 删除ini文件下指定段落下的所有键 /// /// 指定段 public void ClearSection(string Section) { this.IniWriteValue(Section, null, null); } /// /// 读取一个ini里面所有的节 /// /// /// ///
public int GetAllSectionNames(out List
sections) { int MAX_BUFFER = 32767; IntPtr pReturnedString = Marshal.AllocCoTaskMem(MAX_BUFFER); int bytesReturned = GetPrivateProfileSectionNames(pReturnedString, MAX_BUFFER, this.path); if (bytesReturned == 0) { sections = new List
(); return -1; } string local = Marshal.PtrToStringAnsi(pReturnedString, (int)bytesReturned).ToString(); Marshal.FreeCoTaskMem(pReturnedString); //use of Substring below removes terminating null for split sections = local.Substring(0, local.Length - 1).Split('\0').ToList
(); return 0; } ///
/// 得到某个节点下面所有的key和value组合 /// ///
///
///
///
///
public int GetAllKeyValues(string section, out string[] keys, out string[] values) { byte[] b = new byte[65535]; GetPrivateProfileSection(section, b, b.Length, this.path); string s = System.Text.Encoding.Default.GetString(b); string[] tmp = s.Split((char)0); 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 = 0; i < result.Count; i++) { string[] item = result[i].ToString().Split(new char[] { '=' }); if (item.Length == 2) { keys[i] = item[0].Trim(); values[i] = item[1].Trim(); } else if (item.Length == 1) { keys[i] = item[0].Trim(); values[i] = ""; } else if (item.Length == 0) { keys[i] = ""; values[i] = ""; } } return 0; }}

 

转载地址:http://duzpa.baihongyu.com/

你可能感兴趣的文章
页面禁止横屏
查看>>
VS2010调试技巧
查看>>
hdoj2859(矩阵DP)
查看>>
springMVC中跳转问题
查看>>
HTML基础复习(八)表单
查看>>
datagrid分页 从后端获取数据也很简单
查看>>
rowid去重(删除表的重复记录)
查看>>
Java BigDecimal类的使用和注意事项
查看>>
HDU1896 Stones【模拟+优先队列】
查看>>
gulp不完全入门教程
查看>>
互联网网站的反爬虫策略浅析
查看>>
微信教程
查看>>
小组讨论
查看>>
团队作业第二次—项目选题报告
查看>>
docker~docker-compose的使用
查看>>
android 获取系统的参数(如音量大小,背光,网络类型等)
查看>>
lambda表达式
查看>>
[译] 怎样(以及为什么要)保持你的 Git 提交记录的整洁
查看>>
java中主线程等待所有子线程结束
查看>>
JavaScript中call,apply,bind方法的区别
查看>>