你在使用API读取值的时候,把KeyWord参数设为 null 就可以了,返回的是关键字列表,以 null 分隔。比如:
[Window]
Left=10
Top=10
Width=640
Height=480
调用 GetPrivateProfileString("Window", null, bufferStringBuilder, ini 文件名);
后获得的
bufferStringBuilder.ToString() = "Left\0Top\0Width\0Height\0" 用 String.Split 方法可以获得关键字的数组
详细情况可以参看 MSDN 的 Platform SDK 的帮助文档
######################################################33
这个类,封装了Kernal32.dll 中提供的方法来操作ini文件。
简介:
这里创建了一个类,封装了KERNEL32.dll中提供的两个方法,用来操作ini文件。这两个方法是:WritePrivateProfileString 和 GetPrivateProfileString。
需要引用的命名空间为:System.Runtime.InteropServices 和 System.Text
类源文件
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace Ini
{
/**//// <summary>
/// Create a New INI file to store or load data
/// </summary>
public class IniFile
{
public string path;
[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);
/**//// <summary>
/// INIFile Constructor.
/// </summary>
/// <PARAM name="INIPath"></PARAM>
public IniFile(string INIPath)
{
path = INIPath;
}
/**//// <summary>
/// Write Data to the INI File
/// </summary>
/// <PARAM name="Section"></PARAM>
/// Section name
/// <PARAM name="Key"></PARAM>
/// Key Name
/// <PARAM name="value"></PARAM>
/// value Name
public void IniWritevalue(string Section,string Key,string value)
{
WritePrivateProfileString(Section,Key,value,this.path);
}
/**//// <summary>
/// Read Data value From the Ini File
/// </summary>
/// <PARAM name="Section"></PARAM>
/// <PARAM name="Key"></PARAM>
/// <PARAM name="Path"></PARAM>
/// <returns></returns>
public string IniReadvalue(string Section,string Key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section,Key,"",temp, 255, this.path);
return temp.ToString();
}
}
}
使用这个类
按照下列步骤使用:
1. 在你的项目中加入命名空间的引用
using INI;
2. 创建一个如下的INIFile对象
INIFile ini = new INIFile("C:\\test.ini");
3. 使用IniWritevalue方法在指定的配置节给一个键付值,或者使用IniReadvalue方法在指定的一个配置节中读取某个键的值。
如上所述,在C#中非常容易将API函数封装到你的类中。
##########################################################################33
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
本文中介绍的程序设计及运行环境:
● 微软视窗2000 高级服务器版
● .Net Framework SDK正式版
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文件的完整路径和名称。
############################################################
C#对INI文件进行写操作:
对INI文件进行写操作,是通过组件button2的"Click"事件来实现的。这里有一点应该注意,当在调用WritePrivateProfileString()对INI文件进行写操作的时候,如果此时在INI文件中存在和要写入的信息相同的段落名称和关键字,则将覆盖此INI信息。下面是button2组件的"Click"事件对应的代码清单:
private void button2_Click ( object sender , System.EventArgs e )
{
string FileName = textBox1.Text ;
string section = textBox2.Text ;
string key = textBox3.Text ;
string keyValue = textBox4.Text ;
WritePrivateProfileString ( section , key , keyValue , FileName ) ;
MessageBox.Show ( "成功写入INI文件!" , "信息" ) ;
}
C#对INI文件进行读操作:
正确读取INI的必须满足三个前提:INI文件的全路径、段落名称和关键字名称。否则就无法正确读取。你可以设定读取不成功后的缺省数值,在下面的程序中,为了直观设定的是“无法读取对应数值!”字符串,读取INI文件是通过button3组件的“Click”事件来实现的,下面是其对应的代码清单:
private void button3_Click ( object sender , System.EventArgs e )
{
StringBuilder temp = new StringBuilder ( 255 ) ;
string FileName = textBox1.Text ;
string section = textBox2.Text ;
string key = textBox3.Text ;
int i = GetPrivateProfileString ( section , key ,"无法读取对应数值!",
temp , 255 , FileName ) ;
//显示读取的数值
textBox4.Text = temp.ToString ( ) ;
}
############################################
using System ;
没有合适的资源?快使用搜索试试~ 我知道了~
c#小技巧包括窗体启动特效等
共43个文件
txt:9个
xls:5个
exe:4个
需积分: 9 9 下载量 199 浏览量
2009-06-21
02:07:04
上传
评论
收藏 164KB RAR 举报
温馨提示
包括c#窗体启动特效、datagrid添加单选列、socket测试、ini文件的读取和写入、获取文件的当前路径等
资源推荐
资源详情
资源评论
收起资源包目录
C#小技巧.rar (43个子文件)
C#小技巧
获取文件的当前路径.txt 2KB
汉字转拼音.txt 1009B
子线程中修改界面内容.txt 1KB
C#窗体启动特效.txt 1KB
xml文件的读取(空格的处理)
pd_camera.xls 396KB
pd环境记录.txt 85B
pd_t_camera.sql 472KB
摄象机列表(浦东分局)-最新20060515.xls 38KB
pd_camera_pt.xls 31KB
卢湾摄像头整理.xls 22KB
excel_camerainfo
卢湾信号源调整.xls 67KB
ini文件的读取和写入
ini资料杂记.txt 18KB
编解码.txt 1KB
获取datagrid当前选中datarow.txt 316B
datagrid添加单选列.txt 5KB
socket测试
anthorserver
AnthorServer
Server.csproj 5KB
Form1.cs 3KB
bin
Debug
Server.pdb 18KB
Server.exe 20KB
obj
Debug
Server.pdb 18KB
temp
Server.Form1.resources 4KB
Server.projdata 5KB
TempPE
Server.exe 20KB
Server.csproj.user 2KB
Server.sln 897B
App.ico 1KB
Form1.resx 7KB
Server.suo 8KB
AssemblyInfo.cs 2KB
clienta
ClientA
Form1.cs 3KB
bin
Debug
ClientA.pdb 16KB
ClientA.exe 20KB
ClientA.sln 899B
obj
Debug
ClientA.Form1.resources 3KB
ClientA.pdb 16KB
ClientA.exe 20KB
temp
TempPE
ClientA.projdata 4KB
ClientA.csproj.user 2KB
ClientA.csproj 5KB
App.ico 1KB
Form1.resx 6KB
ClientA.suo 9KB
AssemblyInfo.cs 2KB
共 43 条
- 1
资源评论
aaron_hrb
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功