this.controls.addrange ( new system.windows.forms.control [ ] {
this.button3 ,
this.textbox4 ,
this.textbox3 ,
this.textbox2 ,
this.button2 ,
this.textbox1 ,
this.button1 ,
this.label3 ,
this.label2 ,
this.label1 } ) ;
this.maximizebox = false ;
this.name = "form1" ;
this.text = "c#操作ini文件--写操作" ;
this.resumelayout ( false ) ;
}
[stathread]
static void main ( )
{
application.run ( new form1 ( ) ) ;
}
private void button1_click ( object sender , system.eventargs e )
{
openfiledialog1.showdialog ( ) ;
textbox1.text = openfiledialog1.filename ;
}
//写入ini文件
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文件!" , "信息" ) ;
}
//读取指定ini文件的特定段落中的关键字的数值
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 ,
"无法读取对应数值!" , emp , 255 , filename ) ;
//显示读取的数值
textbox4.text = temp.tostring ( ) ;
}
}
}
总结
通过上面的这些介绍,可以看成c#操作ini文件的过程,其实就是c#调用win32的api函数的过程。掌握了如何在c#申明win32的api函数,再来操作ini就显得非常简单。