在.NET下操作INI文件
来源:互联网 发布:青铜器 知乎 编辑:程序博客网 时间:2024/05/21 11:35
using System;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;
namespace Fractalist.Common
{
///
/// 操作INI文件的类
///
public class OperateIni
{
public OperateIni(){}
private const string defaultValue = "无法读取到指定的值";
private static StringBuilder retVal = new StringBuilder(255);
///
/// Win32读取INI文件的API函数
///
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
///
/// 读取INI配置文件的方法
///
/// 段落名称
/// 关键字
/// 无法读取时的缺省值
/// 读取到的值
/// 值大小
/// INI文件的完整路径和名称
///
private static int getPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath)
{
return GetPrivateProfileString(section, key, def, retVal, size, filePath);
}
///
/// Win32修改INI文件的API函数
///
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
///
/// 写入INI配置文件的方法(若为只读则先取消只读文件属性,写入文件,再修改文件属性为只读)
///
/// 段落名称
/// 关键字
/// 待写入的值
/// INI文件的完整路径和名称
///
private static long setPrivateProfileString(string section, string key, string val, string filePath)
{
long longVal = 0;
if((FileAttributes.ReadOnly & File.GetAttributes(filePath)) == FileAttributes.ReadOnly)
{
File.SetAttributes(filePath,FileAttributes.Normal);
longVal = WritePrivateProfileString(section, key, val, filePath);
File.SetAttributes(filePath,FileAttributes.ReadOnly);
}
else
{
longVal = WritePrivateProfileString(section, key, val, filePath);
}
return longVal;
}
private static string getValueOnly(string val)
{
string[] testArray = val.Split(new char[]{'#',});
return testArray[0];
}
}
}
- 在.NET下操作INI文件
- C#下操作ini文件的封装
- .net 下操作 Excel 文件
- ASP.net(C#)下操作XML文件
- .NET下操作注册表
- asp.net 在IIS下操作Excel文件权限不足的解决方法
- asp.net 在IIS下操作Excel文件权限不足的解决方法
- asp.net 在IIS下操作Excel文件权限不足的解决方法
- asp.net 在IIS下操作Excel文件权限不足的解决方法
- 在.NET Framework 2.0即以下平台下操作UDT
- 在linux下操作pcap文件的各种方法整理
- 在Android下操作Excel
- 在telnet下操作memcache
- .net下操作XML的几篇文章
- .Net环境下操作IBM WebShpere MQ
- .net下操作XML的几篇文章
- .Net环境下操作IBM WebShpere MQ
- .net 环境下操作oracle数据库
- 使 ASP.NET Web 站点变得可访问
- 永远的坐票生活真是有趣:如果你只接受最好的,你经常会得到最好的
- Subversion
- 实现 Dispose 方法
- 设计模式c#语言描述——装饰(Decorator)模式
- 在.NET下操作INI文件
- C++字符串完全指引之一 —— Win32 字符编码
- IIS-class
- 我的Blog
- 不要忽视SQL语句中的空格
- 重写 Finalize 方法
- 感受一下,blog的魅力,,哈!
- 工欲善其事,必先利其器——搭建J2ME开发环境
- Hibernate 开发培训教程 中文版