配置文件读写操作(.ini)
来源:互联网 发布:小米5网络制式 编辑:程序博客网 时间:2024/05/04 17:59
使用C#实现对配置文件(config.ini)的读写操作;
以下为基本操作函数源码,包括不同数据类型参数的读写:string、bool、int、double
using System;using System.Collections.Generic;using System.Linq;using System.Windows.Forms;using System.IO;using System.Runtime.InteropServices;using System.Text;public class FileIni { [DllImport("kernel32.dll")] public static extern IntPtr _lopen(string lpPathName, int iReadWrite); [DllImport("kernel32.dll")] public static extern bool CloseHandle(IntPtr hObject); public const int OF_READWRITE = 2; public const int OF_SHARE_DENY_NONE = 0x40; public static readonly IntPtr HFILE_ERROR = new IntPtr(-1); //判断文件是否被占用 占用=true 未占用 = false public static bool isFileOccupied(string path) { if (!File.Exists(path)) return true; IntPtr vHandle = _lopen(path, OF_READWRITE | OF_SHARE_DENY_NONE); if (vHandle == HFILE_ERROR) return true; CloseHandle(vHandle); return false; } [DllImport("kernel32")] private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath); [DllImport("kernel32")] private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); [DllImport("kernel32")] private static extern int GetPrivateProfileInt(string section, string key, int def, string filePath); public static bool isFileExists(string path) { if (!(File.Exists(path))) { MessageBox.Show("文件:" + path + "不存在", "ini文件不存在"); return false; } return true; } //=====================//=====================//=====================Write data #region 写INI参数 public static void WriteString(string path, string section, string key, string value) { // section=配置节,key=键名,value=键值,path=路径 WritePrivateProfileString(section, key, value, path); } public static void WriteInt(string path, string section, string key, int value = 0) { string strRead = ReadString(path, section, key); string[] strArr = strRead.Split(new char[] { ';' }); string strNote = ";"; string strWrite; if (strArr.Length > 1)//保留原有注释 { for (int i = 1; i < strArr.Length; i++) strNote += strArr[i]; strWrite = value.ToString() + strNote; } else { strWrite = value.ToString(); } WritePrivateProfileString(section, key, strWrite, path); } public static void WriteDouble(string path, string section, string key, double value = 0) { string strRead = ReadString(path, section, key); string[] strArr = strRead.Split(new char[] { ';' }); string strNote = ";"; string strWrite; if (strArr.Length > 1)//保留原有注释 { for (int i = 1; i < strArr.Length; i++) strNote += strArr[i]; strWrite = value.ToString() + strNote; } else { strWrite = value.ToString(); } WritePrivateProfileString(section, key, strWrite, path); } public static void WriteBool(string path, string section, string key, bool value = false) { string strValue = (value ? "1" : "0"); string strRead = ReadString(path, section, key); string[] strArr = strRead.Split(new char[] { ';' }); string strNote = ";"; string strWrite; if (strArr.Length > 1)//保留原有注释 { for (int i = 1; i < strArr.Length; i++) strNote += strArr[i]; strWrite = strValue + strNote; } else { strWrite = strValue; } WritePrivateProfileString(section, key, strWrite, path); } #endregion //=====================//=====================//=====================Read data #region 读INI参数 public static string ReadString(string path, string section, string key) { // 每次从ini中读取多少字节 System.Text.StringBuilder temp = new System.Text.StringBuilder(255); // section=配置节,key=键名,temp=上面,path=路径 GetPrivateProfileString(section, key, "", temp, 255, path); String str = temp.ToString(); string[] strArr = str.Split(new char[] { ';' }); string strRead = ""; if (strArr.Length > 0) { strRead = strArr[0]; } return strRead; } public static int ReadInt(string path, string section, string key, int defValue = 0) { return GetPrivateProfileInt(section, key, defValue, path); } public static double ReadDouble(string path, string section, string key, double defValue = 0) { System.Text.StringBuilder temp = new System.Text.StringBuilder(255); GetPrivateProfileString(section, key, defValue.ToString(), temp, 255, path); String str = temp.ToString(); string[] strArr = str.Split(new char[] { ';' }); double ReData; if (strArr.Length > 0) { ReData = Convert.ToDouble(strArr[0]); } else { ReData = defValue; } return ReData; } public static bool ReadBool(string path, string section, string key, int defValue = 0) { int val = GetPrivateProfileInt(section, key, defValue, path); if (val != 0) return true; else return false; } #endregion }
阅读全文
0 0
- 配置文件读写操作(.ini)
- ini配置文件读写操作入门
- Delphi中读写INI配置文件操作总结
- 【工具类】-ini配置文件读写操作类
- 利用GetPrivateProfileString/WritePrivateProfileString读写配置文件(.ini)
- VC配置文件ini的读写(转载)
- 读写配置文件函数(.ini文件)
- VC配置文件ini的读写(转载)
- 学习笔记之一读写配置文件(ini)
- python3 如何读写配置文件(ini)
- ini配置文件读写
- 读写配置文件 ini
- C#读写INI配置文件
- C++读写ini配置文件
- VC++读写INI配置文件
- C#读写ini配置文件
- C++读写ini配置文件
- C#读写ini配置文件
- 自绘控件的4种方法
- [Easy]C++ 第一期 6道
- Freemarker
- Elasticsearch报错:NodeDisconnectedException[[][IP:9300][cluster:monitor/nodes/liveness] disc
- Linux中mysql启动报another mysql daemon already running解决
- 配置文件读写操作(.ini)
- js 获取excel文件并以json形式输出
- js获取浏览器窗口的大小
- App Store 审核条款最新版--iOS审核被拒的原因都在这里了--苹果应用商店审核条款
- 快速学习Markdown
- java解压zip文件到指定位置
- 读书笔记——JAVA核心技术-卷II
- hibernate笔记12 HQL多表查询
- <安彦>Linux系统时间同步