C# 对Ini文件读写
2008-07-30 18:15
381 查看
一、INI文件读写类INICLass
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
namespace IniOperator
{
public class INIClass
{
public string inipath;
[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);
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(500);
int i = GetPrivateProfileString(Section, Key, "", temp, 500, this.inipath);
return temp.ToString();
}
///
/// 验证文件是否存在
///
/// 布尔值
public bool ExistINIFile()
{
return File.Exists(inipath);
}
}
}
二、应用测试
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
namespace IniOperator
{
public partial class Form1 : Form
{
private INIClass inic;
public Form1()
{
InitializeComponent();
}
private void button3_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Ini文件(*.ini)|*.ini|所有文件(*.*)|*.*";
ofd.FilterIndex = 0;
ofd.Title = "打开Ini文件";
if (ofd.ShowDialog() == DialogResult.OK)
{
textBox1.Text = ofd.FileName;
inic = new INIClass(textBox1.Text);
}
}
private void button1_Click(object sender, EventArgs e)
{
if (inic.ExistINIFile())
{
try
{
inic.IniWriteValue(textBox2.Text, textBox3.Text, textBox4.Text);
this.toolStripStatusLabel1.Text = "写入成功";
}
catch (Exception ex)
{
this.toolStripStatusLabel1.Text = ex.Message;
}
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
textBox4.Text = inic.IniReadValue(textBox2.Text, textBox3.Text);
this.toolStripStatusLabel1.Text = textBox4.Text;
}
catch (Exception ex)
{
this.toolStripStatusLabel1.Text = ex.Message;
}
}
private void Form1_Load(object sender, EventArgs e)
{
string name = Dns.GetHostName();
this.toolStripStatusLabel1.Text = name;
IPHostEntry me = Dns.GetHostByName(name);
foreach (IPAddress ip in me.AddressList)
{ this.toolStripStatusLabel1.Text = this.toolStripStatusLabel1.Text+" IP:"+ip.ToString(); }
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
namespace IniOperator
{
public class INIClass
{
public string inipath;
[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);
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(500);
int i = GetPrivateProfileString(Section, Key, "", temp, 500, this.inipath);
return temp.ToString();
}
///
/// 验证文件是否存在
///
/// 布尔值
public bool ExistINIFile()
{
return File.Exists(inipath);
}
}
}
二、应用测试
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
namespace IniOperator
{
public partial class Form1 : Form
{
private INIClass inic;
public Form1()
{
InitializeComponent();
}
private void button3_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Ini文件(*.ini)|*.ini|所有文件(*.*)|*.*";
ofd.FilterIndex = 0;
ofd.Title = "打开Ini文件";
if (ofd.ShowDialog() == DialogResult.OK)
{
textBox1.Text = ofd.FileName;
inic = new INIClass(textBox1.Text);
}
}
private void button1_Click(object sender, EventArgs e)
{
if (inic.ExistINIFile())
{
try
{
inic.IniWriteValue(textBox2.Text, textBox3.Text, textBox4.Text);
this.toolStripStatusLabel1.Text = "写入成功";
}
catch (Exception ex)
{
this.toolStripStatusLabel1.Text = ex.Message;
}
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
textBox4.Text = inic.IniReadValue(textBox2.Text, textBox3.Text);
this.toolStripStatusLabel1.Text = textBox4.Text;
}
catch (Exception ex)
{
this.toolStripStatusLabel1.Text = ex.Message;
}
}
private void Form1_Load(object sender, EventArgs e)
{
string name = Dns.GetHostName();
this.toolStripStatusLabel1.Text = name;
IPHostEntry me = Dns.GetHostByName(name);
foreach (IPAddress ip in me.AddressList)
{ this.toolStripStatusLabel1.Text = this.toolStripStatusLabel1.Text+" IP:"+ip.ToString(); }
}
}
}
相关文章推荐
- C#中读写INI配置文件
- 纯C#的ini格式配置文件读写
- 用C#读写ini配置文件
- C#读写ini文件操作
- c# 读写INI配置文件 张宇轩
- C# API读写ini配置文件
- C#读写INI配置文件(转)
- Windows系统中C#读写ini配置文件的程序代码示例分享
- C#读写.ini文件
- C#常用操作类库二(ini配置文件读写)
- 用C#读写ini配置文件
- C# ini文件读写基本操作
- C#对ini配置文件的读写
- [WPF]C#读写ini配置文件【个人学习用】
- C# 对Ini文件读写
- C# 读写Ini 文件
- 用C#读写ini配置文件(转自:http://www.cnblogs.com/zzyyll2/archive/2007/11/06/950584.html)
- 用C#读写ini配置文件完整范例
- C#读写INI配置文件
- 在C#中读写INI配置文件