用C#读写ini文件

可以通过调用kernel32.dll中的两个api:WritePrivateProfileString,GetPrivateProfileString来实现对ini 文件的读些。

具体实现的代码如下:

None.gif using  System;
None.gif
using  System.Drawing;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Windows.Forms;
None.gif
using  System.Data;
None.gif
using  System.Runtime.InteropServices;
None.gif
using  System.Text;
None.gif
None.gif
None.gif
namespace  iniprocess
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif 
InBlock.gif 
public class Form1 : System.Windows.Forms.Form
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  
private System.Windows.Forms.TextBox textBox1;
InBlock.gif  
private System.Windows.Forms.Button button2;
InBlock.gif  
private System.Windows.Forms.Button button1;
InBlock.gif  
InBlock.gif  [DllImport(
"kernel32")]
InBlock.gif  
private static extern long WritePrivateProfileString(string section,
InBlock.gif   
string key,string val,string filePath);
InBlock.gif  [DllImport(
"kernel32")]
InBlock.gif  
private static extern int GetPrivateProfileString(string section,
InBlock.gif   
string key,string def, StringBuilder retVal,
InBlock.gif   
int size,string filePath);
InBlock.gif
InBlock.gif  
public void IniWriteValue(string Section,string Key,string Value,string filepath)//对ini文件进行写操作的函数
ExpandedSubBlockStart.gifContractedSubBlock.gif
  dot.gif{
InBlock.gif   WritePrivateProfileString(Section,Key,Value,filepath);
ExpandedSubBlockEnd.gif  }

InBlock.gif        
InBlock.gif  
public string IniReadValue(string Section,string Key,string filepath)//对ini文件进行读操作的函数
ExpandedSubBlockStart.gifContractedSubBlock.gif
  dot.gif{
InBlock.gif   StringBuilder temp 
= new StringBuilder(255);
InBlock.gif   
int i = GetPrivateProfileString(Section,Key,"",temp, 
InBlock.gif    
255, filepath);
InBlock.gif   
return temp.ToString();
InBlock.gif
ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif
InBlock.gif  
InBlock.gif
InBlock.gif  
private void button1_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
InBlock.gif   
this.textBox1 .Text= IniReadValue("ODBC 32 bit Data Sources","MS Access Database","e:\\temp\\ODBC.INI");
InBlock.gif  
ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif  
private void button2_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
InBlock.gif   IniWriteValue (
"ODBC 32 bit Data Sources","MS Access Database",this.textBox1 .Text,"e:\\temp\\ODBC.INI");
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

None.gif


转载:http://www.sxna.cn/tech/14/1/41041.shtml

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值