c#读写ini文件
2016-01-22 17:56
435 查看
通过调用win32 api可以直接读写ini文件
写了一个助手类方便操作:
IniFileHelper
测试代码:
运行结果:
读取测试:
写了一个助手类方便操作:
using System; using System.Text; using System.Runtime.InteropServices; public static class IniFileHelper { [DllImport("kernel32")] private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); [DllImport("kernel32")] private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath); public static void Write(string section, string key, string val, string filePath) { WritePrivateProfileString(section, key, val, filePath); } public static string Read(string section, string key, string filePath) { var stringBuilder = new StringBuilder(255); GetPrivateProfileString(section, key, string.Empty, stringBuilder, 255, filePath); return stringBuilder.ToString(); } }
IniFileHelper
测试代码:
static void Main(string[] args) { IniFileHelper.Write("BaseSetting", "Language", "en_us", "./Config.ini"); IniFileHelper.Write("Sound", "Enable", "True", "./Config.ini"); IniFileHelper.Write("Sound", "Volume", "100", "./Config.ini"); }
运行结果:
读取测试:
相关文章推荐
- MangoDB的C#Driver驱动简单例子
- C#选择文件、选择文件夹、打开文件夹
- C# WinForm控件的拖动和缩放的实现
- ListView列表视图控件
- c# 读取 XML
- C#遍历/反射 属性/字段
- c#xml序列化对象,xml标记都缩写了
- C# 做ActiveX控件问题
- c#解析xml字符串 分析 EntityName 时出错
- C#中new和override的区别
- C#为IE编写BHO插件心得
- C#
- C#高效编程改善C#程序的50种方法 读书笔记
- C#基础
- 左倾堆(C#)
- C#中类的实例是不能 获取到类中的静态方法和静态变量(Static)的,及原因
- C#中为什么不能再方法里定义静态变量(Static)
- 跟siki老师学C#第六天(练习部分)
- C# Winform 中tabControl的使用
- C#中增加弹出对话框控件的方法