如何在C#中读写INI文件

如何在C#中读写INI文件,第1张

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

BussinessSkinForm我记得可以把风格写进程序里面的,有一个属性改一下就可以了,好久没用忘记啦!

下面是ini的读写

内容:

INI文件在系统配置及应用程序参数保存与设置方面,具有很重要的作用,所以可视化的编程一

族,如VB、VC、VFP、Delphi等都提供了读写INI文件的方法,其中Delphi中 *** 作INI文件,最为

简洁,这是因为Delphi6.0提供了一个TInifile类,使我们可以非常灵活的处理INI文件。

一、有必要了解INI文件的结构:

注释

[小节名]

关键字=值

...

INI文件允许有多个小节,每个小节又允许有多个关键字, “=”后面是该关键字的值。

值的类型有三种:字符串、整型数值和布尔值。其中字符串存贮在INI文件中时没有引号,布尔

真值用1表示,布尔假值用0表示。

注释以分号“”开头。

二、定义

1、在Interface的Uses节增加IniFiles;

2、在Var变量定义部分增加一行:

myinifile:Tinifile

然后,就可以对变量myinifile进行创建、打开、读取、写入等 *** 作了。

三、打开INI文件

myinifile:=Tinifile.create(’program.ini’)

上面这一行语句将会为变量myinifile与具体的文件 program.ini建立联系,然后,就可以通过

变量myinifile,来读写program.ini文件中的关键字的值了。

值得注意的是,如果括号中的文件名没有指明路径的话,那么这个Program.ini文件会存储

在Windows目录中,把Program.ini文件存储在应用程序当前目录中的方法是:为其指定完整的

路径及文件名。下面的两条语句可以完成这个功

能:

Filename:=ExtractFilePath(Paramstr(0))+’program.ini’

myinifile:=Tinifile.Create(filename)

四、读取关键字的值

针对INI文件支持的字符串、整型数值、布尔值三种数据类型,TINIfiles类提供了三种不同的

对象方法来读取INI文件中关键字的值。

假设已定义变量vs、vi、vb分别为string、 integer、boolean类型。

vs:=myinifile.Readstring(’小节名’,’关键字’,缺省值)

vi:=myinifile.Readinteger(’小节名’,’关键字’,缺省值)

vb:=myinifile.Readbool(’小节名’,’关键字’,缺省值)

其中缺省值为该INI文件不存在该关键字时返回的缺省值。

五、写入INI文件

同样的,TInifile类也提供了三种不同的对象方法,向INI文件写入字符串、整型数及布尔类型的关键字。

myinifile.writestring(’小节名’,’关键字’,变量或字符串值)

myinifile.writeinteger(’小节名’,’关键字’,变量或整型数值)

myinifile.writebool(’小节名’,’关键字’,变量或True或False)

当这个INI文件不存在时,上面的语句还会自动创建该INI文件。

六、删除关键字

除了可用写入方法增加一个关键字,Tinifile类还提供了一个删除关键字的对象方法:

myinifile.DeleteKey(’小节名’,’关键字’)

七、小节 *** 作

增加一个小节可用写入的方法来完成,删除一个小节可用下面的对象方法:

myinifile.EraseSection(’小节名’)

另外Tinifile类还提供了三种对象方法来对小节进行 *** 作:

myinifile.readsection(’小节名’,TStrings变量)可将指定小节中的所有关键字名读取至

一个字符串列表变量中;

myinifile.readsections(TStrings变量)可将INI文件中所有小节名读取至一个字符串列表变量中去。

myinifile.readsectionvalues(’小节名’,TStrings变量)可将INI文件中指定小节的所有

行(包括关键字、=、值)读取至一个字符串列表变量中去。

八、释放

在适当的位置用下面的语句释放myinifile:

myinifile.distory


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/11422574.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-16
下一篇 2023-05-16

发表评论

登录后才能评论

评论列表(0条)

保存