读写INI
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace CoolSoftClient
{
//DoIniFile 的摘要说明。
public class DoIniFile
{
public string path; //INI文件名
public DoIniFile(string INIPath)
{
path = INIPath;
}
//声明读写INI文件的API函数
[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);
//写INI文件
public void IniWriteValue(string Section,string Key,string Value)
{
WritePrivateProfileString(Section,Key,Value,this.path);
}
//读取INI文件指定
public string IniReadValue(string Section,string Key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section,Key,"",temp,255,this.path);
return temp.ToString();
}
}
}
调用方法:
DoIniFile IniClass = new DoIniFile(Path);
//声明
IniClass.IniWriteValue("NetSetting","NetWorkAllUrl",textBox1.Text);
//写数据
textBox1.Text = ini.IniReadValue("NetSetting","NetWorkAllUrl");
//读数据
无意中先到怎么能读写一个ini的配置文件,在网上查了写,随意记录下了备用
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace CoolSoftClient
{
//DoIniFile 的摘要说明。
public class DoIniFile
{
public string path; //INI文件名
public DoIniFile(string INIPath)
{
path = INIPath;
}
//声明读写INI文件的API函数
[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);
//写INI文件
public void IniWriteValue(string Section,string Key,string Value)
{
WritePrivateProfileString(Section,Key,Value,this.path);
}
//读取INI文件指定
public string IniReadValue(string Section,string Key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section,Key,"",temp,255,this.path);
return temp.ToString();
}
}
}
调用方法:
DoIniFile IniClass = new DoIniFile(Path);
//声明
IniClass.IniWriteValue("NetSetting","NetWorkAllUrl",textBox1.Text);
//写数据
textBox1.Text = ini.IniReadValue("NetSetting","NetWorkAllUrl");
//读数据