在C#中读写INI配置文件
2015-07-24 16:24
399 查看
using System;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
namespace PubOp
{
public class OperateIniFile
{
#region API函数声明
}
简单说明以下方法WriteIniData()和ReadIniData()的参数。
Section参数、Key参数和IniFilePath不用再说,Value参数表明key的值,而这里的NoText对应API函数的def参数,它的值由用户指定,是当在配置文件中没有找到具体的Value时,就用NoText的值来代替。
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
namespace PubOp
{
public class OperateIniFile
{
#region API函数声明
[DllImport("kernel32")]//返回0表示失败,非0为成功 private static extern long WritePrivateProfileString(string section,string key, string val,string filePath); [DllImport("kernel32")]//返回取得字符串缓冲区的长度 private static extern long GetPrivateProfileString(string section,string key, string def,StringBuilder retVal,int size,string filePath); #endregion #region 读Ini文件 public static string ReadIniData(string Section,string Key,string NoText,string iniFilePath) { if(File.Exists(iniFilePath)) { StringBuilder temp = new StringBuilder(1024); GetPrivateProfileString(Section,Key,NoText,temp,1024,iniFilePath); return temp.ToString(); } else { return String.Empty; } } #endregion #region 写Ini文件 public static bool WriteIniData(string Section,string Key,string Value,string iniFilePath) { if(File.Exists(iniFilePath)) { long OpStation = WritePrivateProfileString(Section,Key,Value,iniFilePath); if(OpStation == 0) { return false; } else { return true; } } else { return false; } } #endregion }
}
简单说明以下方法WriteIniData()和ReadIniData()的参数。
Section参数、Key参数和IniFilePath不用再说,Value参数表明key的值,而这里的NoText对应API函数的def参数,它的值由用户指定,是当在配置文件中没有找到具体的Value时,就用NoText的值来代替。
相关文章推荐
- delphi建立、读取、存贮INI文件的方法《三》
- delphi建立、读取、存贮INI文件的方法《二》
- Delphi建立、读取、存贮INI文件的方法《一》
- Powershell Profiles配置文件的存放位置介绍
- PowerShell入门教程之创建和使用配置文件实例
- C#中读写INI文件的方法例子
- C#实现读写ini文件类实例
- C++读取INI配置文件类实例详解
- SERV-U 的配置文件详解
- php中的ini配置原理详解
- php中动态修改ini配置
- php.ini 配置文件的深入解析
- redis2.8配置文件中文翻译版
- PHP配置文件php.ini中打开错误报告的设置方法
- C#中读取App.config配置文件代码实例
- c++实现逐行读取配置文件写入内存的示例
- 关于session在PHP5的配置文件中的详细设置参数说明
- MySQL 配置文件my.cnf 例子最详细翻译
- apache 配置文件解说
- Android 读取Properties配置文件的小例子