`
tudusi
  • 浏览: 1041788 次
文章分类
社区版块
存档分类
最新评论

如何在C#中读写INI文件

 
阅读更多
INI文件就是扩展名为“ini”的文件。在Windows系统中,INI文件是很多,最重要的就是“System.ini”、“System32.ini”和“Win.ini”。
该文件主要存放用户所做的选择以及系统的各种参数。用户可以通过修改INI文件,来改变应用程序和系统的很多配置。但自从Windows 95的退出,
在Windows系统中引入了注册表的概念,INI文件在Windows系统的地位就开始不断下滑,这是因为注册表的独特优点,使应用程序和系统都把许多参数和
初始化信息放进了注册表中。但在某些场合,INI文件还拥有其不可替代的地位。本文就来探讨一下C#是如何对INI进行读写操作。


INI文件的结构
INI文件是一种按照特定方式排列的文本文件。每一个INI文件构成都非常类似,由若干段落(section)组成,在每个带括号的标题下面,
是若干个以单个单词开头的关键词(keyword)和一个等号,等号右边的就是关键字对应的值(value)。其一般形式如下:
[Section1]
  KeyWord1 = Valuel
  KeyWord2 = Value2
   ……
  [Section2]
  KeyWord3 = Value3
  KeyWord4 = Value4


C#和Win32 API函数


C#并不像C++,拥有属于自己的类库。C#使用的类库是.Net框架为所有.Net程序开发提供的一个共有的类库——.Net FrameWork SDK。
虽然.Net FrameWork SDK内容十分庞大,功能也非常强大,但还不能面面俱到,至少它并没有提供直接操作INI文件所需要的相关的类。在本文中,
C#操作INI文件使用的是Windows系统自带Win32的API函数——WritePrivateProfileString()和GetPrivateProfileString()函数。这二个函数都位于
“kernel32.dll”文件中。


我们知道在C#中使用的类库都是托管代码(Managed Code)文件,而Win32的API函数所处的文件,都是非托管代码(Unmanaged Code)文件。
这就导致了在C#中不可能直接使用这些非托管代码文件中的函数。好在.Net框架为了保持对下的兼容,也为了充分利用以前的资源,提出了互操作,
通过互操作可以实现对Win32的API函数的调用。互操作不仅适用于Win32的API函数,还可以用来访问托管的COM对象。
C#中对Win32的API函数的互操作是通过命名空间“System.Runtime.InteropServices”中的“DllImport”特征类来实现的。
它的主要作用是指示此属性化方法是作为非托管DLL的输出实现的。
下面代码就是在C#利用命名空间“System.Runtime.InteropServices”中的“DllImport”特征类申明上面二个Win32的API函数:


C#申明INI文件的写操作函数WritePrivateProfileString():
[ DllImport ( "kernel32" ) ]
private static extern long WritePrivateProfileString ( string section , string key , string val , string filePath ) ;


参数说明:section:INI文件中的段落;key:INI文件中的关键字;val:INI文件中关键字的数值;filePath:INI文件的完整的路径和名称。
C#申明INI文件的读操作函数GetPrivateProfileString():
[ DllImport ( "kernel32" ) ]
private static extern int GetPrivateProfileString ( string section ,string key , string def , StringBuilder retVal , int size , string filePath ) ;


参数说明:section:INI文件中的段落名称;key:INI文件中的关键字;def:无法读取时候时候的缺省数值;retVal:读取数值;size:数值的大小;
filePath:INI文件的完整路径和名称。


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;


namespace INI.cs
{
class INI
{
//定义系统时间的信息结构,用于接收从系统中获取的日期和时间
[StructLayout(LayoutKind.Sequential)]
public struct SYSTEMTIME_INFO
{
public ushort wYear;
public ushort wMonth;
public ushort wDayOfWeek;
public ushort wDay;
public ushort wHour;
public ushort wMinute;
public ushort wSecond;
public ushort wMilliseconds;
}


//声明将要使用的Win32的API函数
[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);
[DllImport("kernel32")]
private static extern void GetWindowsDirectory(StringBuilder WinDir, int count);
[DllImport("kernel32")]
public static extern void GetSystemDirectory(StringBuilder SysDir, int count);
[DllImport("kernel32")]
public static extern void GetLocalTime(ref SYSTEMTIME_INFO stinfo);


static void Main(string[] args)
{
StringBuilder wd = new StringBuilder(128);
StringBuilder sd = new StringBuilder(128);
SYSTEMTIME_INFO StInfo = new SYSTEMTIME_INFO();


//获取指定系统信息
GetWindowsDirectory(wd, 128);
GetSystemDirectory(sd, 128);
GetLocalTime(ref StInfo);


string date = StInfo.wYear.ToString() + "年" + StInfo.wMonth.ToString() + "月" + StInfo.wDay.ToString() + "日";
string time = (StInfo.wHour).ToString() + "点" + StInfo.wMinute.ToString() + "分" + StInfo.wSecond.ToString() + "秒";


//写入INI文件
WritePrivateProfileString("Directory", "WindowsDirectory", wd.ToString(), "c:\\sys.ini");
WritePrivateProfileString("Directory", "SystemDirectory", sd.ToString(), "c:\\sys.ini");
WritePrivateProfileString("Time", "SystemTime", date +" "+time, "c:\\sys.ini");


//读取INI文件
StringBuilder wd1 = new StringBuilder(128);
StringBuilder sd1 = new StringBuilder(128);
int i = GetPrivateProfileString("Directory", "WindowsDirectory", "无法读取对应数值!", wd1, 128, "c:\\sys.ini");//返回值是读取的字符个数
int j = GetPrivateProfileString("Directory", "SystemDirectory", "无法读取对应数值!", sd1, 128, "c:\\sys.ini");


Console.WriteLine(i + " " + wd1);
Console.WriteLine(j + " " + sd1);


Console.ReadKey();
}
}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics