INI文件就是扩展名为 ini 的文件 在Windows系统中 INI文件是很多 最重要的就是 System ini System ini 和 Win ini 该文件主要存放用户所做的选择以及系统的各种参数 用户可以通过修改INI文件 来改变应用程序和系统的很多配置 但自从Windows 的退出 在Windows系统滑大中引入了注册表的概念 INI文件在Windows系统的地位就开始不断下滑 这是因为注册表的独特优点 使应用程序和系统都把许多参数和初始化信息放进了注册表中 但在某些场合 INI文件还拥有其不可替代的地位 本文就来探讨一下C#是如何对INI进行读写 *** 作 INI文件的结构 INI文件是一种按照特点方式排列的文本文件 每一个INI文件构成都非常类似 由若干段落(section)组迟老成 在每个带括号的标题下面 是若干个以单个单词开头的关键词(keyword)和一个等号 等号右边的就是关键字对应的值(value) 其一般形式如下
[Section ] KeyWord = Valuel KeyWord = Value …… [Section ] KeyWord = Value KeyWord = Value
C#和Win API函数 C#并不像C++ 拥有属于自己的类库 C#使用的类库是 Net框架为所有 Net程序开发提供的一个共有的类库—— Net FrameWork SDK 虽然 Net FrameWork SDK内容十分庞大 功能也非常强大 但还不能面面俱到 至少它并没有提供直接 *** 作INI文件所需要的相关的类 在本文中 C# *** 作INI文件使用的是Windows系统自带Win 的API函数——WritePrivateProfileString()和GetPrivateProfileString()函数 这二个函数都位于 kernel dll 文件中 我们知道在C#中使用的类库都是托管代码(Managed Code)文件 而Win 的API函数所处的文件 都是非托管代码(Unmanaged Code)文件 这就导致了在C#中不可能直接使用这些非托管代码文件中的函数 好在 Net框架为了保持对下的兼容 也为了充分利用以前的资源 提出了互 *** 作 通过互 *** 作可以实现对Win 的API函数的调用 互 *** 作不仅适用于Win 的API函数 还可以用来访问托管的对象 C#中对Win 的API函数的互 *** 作是通过命名空间 System Runtime InteropServices 中的 DllImport 特征类来实现的 它的主要作用是指示此属性化方法是作为非托管DLL的输出实现的 下面代码就是在C#利用命名空间 System Runtime InteropServices 中的 DllImport 特征类申明上面二个Win 的API函数 C#申明INI文件的写 *** 作函数WritePrivateProfileString() [ DllImport ( kernel ) ] 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 ( kernel ) ] 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文件的完整路径和名称 下面是一个读写INI文件的类
public class INIClass { public string inipath[DllImport( kernel )] private static extern long WritePrivateProfileString(string section string key string val string filePath)[DllImport( kernel )] private static extern int GetPrivateProfileString(string section string key string def StringBuilder retVal int size string filePath)///
/// 构造方法 /// ///文件路径
public INIClass(string INIPath) { inipath = INIPath} /// /// 写入INI文件 /// ///
项目名称(如 [TypeName] )
///键
///值
public void IniWriteValue(string Section string Key string Value) { WritePrivateProfileString(Section Key Value this inipath)} /// /// 读出INI文件 /// ///项目名称(如 [TypeName] )
///键
public string IniReadValue(string Section string Key) { StringBuilder temp = new StringBuilder( )int i = GetPrivateProfileString(Section Key temp this inipath)return temp ToString()} /// /// 验证文件是否存在 /// /// 布尔值 public bool ExistINIFile() { return File Exists(inipath)} }
C#对INI文件进行写 *** 作 对INI文件进行写 *** 作 是通过组件button 的 Click 事件来实现的 这里有一点应该注意 当在调用WritePrivateProfileString()对INI文件进行写 *** 作的时候 如果此时在INI文件中存在和要写入的信息相同的段落名称和关键字 则将覆盖此INI信息 下面是button 组件的 Click 事件对应的代码清单
private void button _Click ( object sender System EventArgs e )
{
string FileName = textBox Text
string section = textBox Text
string key = textBox Text
string keyValue = textBox Text
WritePrivateProfileString ( section key keyValue FileName )
MessageBox Show ( 成功写入INI文件! 信息 )
}
C#对INI文件进行读 *** 作 正确读取INI的必须满足三个前提 INI文件的全路径 段落名称和关键字名称 否则就无法正确读取 你可以设定读取不成功后的缺省数值 在下面的程序中 为了直观设定的是 无法读取对应数值! 字符串 读取INI文件是通过button 组件的 Click 事件来实现的 下面是其对应的代码清单
private void button _Click ( object sender System EventArgs e )
{
StringBuilder temp = new StringBuilder ( )
string FileName = textBox Text
string section = textBox Text
string key = textBox Text
int i = GetPrivateProfileString ( section key 无法读取对应数值!
temp FileName )
//显示读取的数值
textBox Text = temp ToString ( )
}
using System
using System Drawing
using System Collections
using System ComponentModel
using System Windows Forms
using System Data
using System Runtime InteropServices
using System Text
namespace C_ *** 作INI文件__写 *** 作
{
public class Form : System Windows Forms Form
{
private System Windows Forms Button button
private System Windows Forms TextBox textBox
private System Windows Forms Button button
private System Windows Forms TextBox textBox
private System Windows Forms TextBox textBox
private System Windows Forms TextBox textBox
private System Windows Forms Label label
private System Windows Forms Label label
private System Windows Forms Label label
private System Windows Forms Button button
private System Windows Forms OpenFileDialog openFileDialog
private System ComponentModel Container ponents = null
public Form ( )
{
InitializeComponent ( )
}
protected override void Dispose ( bool disposing )
{
if ( disposing )
{
if ( ponents != null )
{
ponents Dispose ( )
}
}
base Dispose ( disposing )
}
[ DllImport ( kernel ) ]
private static extern long WritePrivateProfileString ( string
section
string key string val string filePath )
[ DllImport ( kernel ) ]
private static extern int GetPrivateProfileString ( string section
string key string def StringBuilder retVal
int size string filePath )
private void InitializeComponent ( )
{
this button = new System Windows Forms Button ( )
this textBox = new System Windows Forms TextBox ( )
this button = new System Windows Forms Button ( )
this textBox = new System Windows Forms TextBox ( )
this textBox = new System Windows Forms TextBox ( )
this textBox = new System Windows Forms TextBox ( )
this label = new System Windows Forms Label ( )
this label = new System Windows Forms Label ( )
this label = new System Windows Forms Label ( )
this button = new System Windows Forms Button ( )
this openFileDialog = new
System Windows Forms OpenFileDialog ( )
this SuspendLayout ( )
this button FlatStyle = System Windows Forms FlatStyle Flat
this button Location = new System Drawing Point ( )
this button Name = button
this button Size = new System Drawing Size ( )
this button TabIndex =
this button Text = 选择INI文件
this button Click += new System EventHandler ( this button _Click )
this textBox Location = new System Drawing Point ( )
this textBox Name = textBox
this textBox Size = new System Drawing Size ( )
this textBox TabIndex =
this textBox Text =
this button FlatStyle = System Windows Forms FlatStyle Flat
this button Location = new System Drawing Point ( )
this button Name = button
this button Size = new System Drawing Size ( )
this button TabIndex =
this button Text = 写入INI文件
this button Click += new System EventHandler ( this button _Click )
this textBox Location = new System Drawing Point ( )
this textBox Name = textBox
this textBox Size = new System Drawing Size ( )
this textBox TabIndex =
this textBox Text =
this textBox Location = new System Drawing Point ( )
this textBox Name = textBox
this textBox Size = new System Drawing Size ( )
this textBox TabIndex =
this textBox Text =
this textBox Location = new System Drawing Point ( )
this textBox Name = textBox
this textBox Size = new System Drawing Size ( )
this textBox TabIndex =
this textBox Text =
this label Location = new System Drawing Point ( )
this label Name = label
this label TabIndex =
this label Text = 段落名称
this label Location = new System Drawing Point ( )
this label Name = label
this label TabIndex =
this label Text = 关键字
this label Location = new System Drawing Point ( )
this label Name = label
this label TabIndex =
this label Text = 关键字数值
this button FlatStyle = System Windows Forms FlatStyle Flat
this button Location = new System Drawing Point ( )
this button Name = button
this button Size = new System Drawing Size ( )
this button TabIndex =
this button Text = 读取INI数值
this button Click += new System EventHandler ( this button _Click )
this openFileDialog Filter = INI 文件|* ini
this AutoScaleBaseSize = new System Drawing Size ( )
this ClientSize = new System Drawing Size ( )
this Controls AddRange ( new System Windows Forms Control [ ] {
this button
this textBox
this textBox
this textBox
this button
this textBox
this button
this label
this label
this label } )
this MaximizeBox = false
this Name = Form
this Text = C# *** 作INI文件 写 *** 作
this ResumeLayout ( false )
}
[STAThread]
static void Main ( )
{
Application Run ( new Form ( ) )
}
private void button _Click ( object sender System EventArgs e )
{
openFileDialog ShowDialog ( )
textBox Text = openFileDialog FileName
}
//写入INI文件
private void button _Click ( object sender System EventArgs e )
{
string FileName = textBox Text
string section = textBox Text
string key = textBox Text
string keyValue = textBox Text
WritePrivateProfileString ( section key keyValue FileName )
MessageBox Show ( 成功写入INI文件! 信息 )
}
//读取指定INI文件的特定段落中的关键字的数值
private void button _Click ( object sender System EventArgs e )
{
this Controls AddRange ( new System Windows Forms Control [ ] {
this button
this textBox
this textBox
this textBox
this button
this textBox
this button
this label
this label
this label } )
this MaximizeBox = false
this Name = Form
this Text = C# *** 作INI文件 写 *** 作
this ResumeLayout ( false )
}
[STAThread]
static void Main ( )
{
Application Run ( new Form ( ) )
}
private void button _Click ( object sender System EventArgs e )
{
openFileDialog ShowDialog ( )
textBox Text = openFileDialog FileName
}
//写入INI文件
private void button _Click ( object sender System EventArgs e )
{
string FileName = textBox Text
string section = textBox Text
string key = textBox Text
string keyValue = textBox Text
WritePrivateProfileString ( section key keyValue FileName )
MessageBox Show ( 成功写入INI文件! 信息 )
}
//读取指定INI文件的特定段落中的关键字的数值
private void button _Click ( object sender System EventArgs e )
{
StringBuilder temp = new StringBuilder ( )
string FileName = textBox Text
string section = textBox Text
string key = textBox Text
int i = GetPrivateProfileString ( section key
无法读取对应数值! emp FileName )
//显示读取的数值
textBox Text = temp ToString ( )
}
}
}
总结
通过上面的这些介绍 可以看成C# *** 作INI文件的过程 其实就是C#调用Win 的API函数的过程 掌握了如何在C#申明Win 的API函数 再来 *** 作INI就显得非常简单
lishixinzhi/Article/program/ASP/201311/21793
利用系统API函数 GetPrivateProfileString 可以方便地读取ini文件。使用方法如下
(1)MyApp.INI文件的内容为
VB程序读取这个ini文件,将窗口的标题换为银哪Title指定的字符串
(2)新建一个VB工程
(3)Form1窗体代码
Option ExplicitPrivate Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringW" _
(ByVal lpApplicationName As Long, _
ByVal lpKeyName As Long, _
ByVal lpDefault As Long, _
ByVal lpReturnedString As Long, _
ByVal nSize As Long, _
ByVal lpFileName As Long) As Long
模厅
'------------
'读INI文件
'------------
Private Function GetValueFromINIFile(ByVal SectionName As String, _
ByVal KeyName As String, _
ByVal IniFileName As String) As String
Dim strBuf As String
'128个字符,初始化时用 0 填充
strBuf = String(128, 0)
GetPrivateProfileString StrPtr(SectionName), _
StrPtr(KeyName), _
StrPtr(""), _
StrPtr(strBuf), _
128, _
StrPtr(IniFileName)
'去除多余的 0
strBuf = Replace(strBuf, Chr(0), "")
GetValueFromINIFile = strBuf
End Function
Private Sub Form_Load()
Dim title As String
'读取INI文件中指定的节和节/键
'节的名称:AppName
'键名称:Title
title = GetValueFromINIFile("AppName", "Title", "c:\MyApp.INI")
旦搏隐 Me.Caption = title
End Sub
(4)运行效果
窗口的标题被设置Ini文件指定的字符串!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)