c#如何读INI文件中的设置信息
来源:互联网 发布:chm阅读软件 编辑:程序博客网 时间:2024/06/06 03:27
原文:c#如何读INI文件中的设置信息
//write by www.wenhui.org
把下面的代码改动一下,就可以在你的程序中使用,当然
别忘记加上名字空间哦。
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace Sx_Mdi
{
/// <summary>
/// Summary des cription for Class1.
/// </summary>
public class IniFile
{
//文件INI名称
public string Path;
////声明读写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 IniFile(string inipath)
{
//
// TODO: Add constructor logic here
//
Path = inipath;
}
//写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();
}
}
}
操作范例:
public static SqlConnection MyConnection()
{
string sPath;
string ServerName,userId,sPwd,DataName;
sPath = GetPath();
IniFile ini = new IniFile(sPath);
ServerName = ini.IniReadValue ("Database","server");
userId = ini.IniReadValue ("Database","uid");
sPwd = ini.IniReadValue ("Database","pwd");
DataName = ini.IniReadValue ("Database","database");
string strSql = "server =" + ServerName+";uid ="+ userId +";pwd =;database ="+ DataName;
SqlConnection myConn=new SqlConnection(strSql);
return myConn;
}
- c#如何读INI文件中的设置信息
- c#如何读INI文件中的设置信息
- c#如何读INI文件中的设置信息
- c#如何读INI文件中的设置信息
- c#如何操作ini文件
- C#如何读写ini文件
- C#如何读写ini文件
- php.ini文件中的include_path设置
- 在C#中如何读写INI文件
- C#中如何读写INI文件
- C#中如何读写INI文件
- C#如何读写和创建INI文件
- C#如何读写和创建INI文件
- C#如何读写和创建INI文件
- C#中如何读写INI文件
- C#如何读写和创建INI文件
- C#如何读写和创建INI文件
- 如何在C#中读写INI文件
- IP ROUTE2 命令
- 如何生成代理/存根dll
- java 实现将传入的两个集合进行并,交,联集,差操作
- 提升安全性Win XP必禁的十大服务
- 网络攻击常用的命令
- c#如何读INI文件中的设置信息
- xmlhttp实现的三级连动下拉菜单(无刷新)经测试,有bug。无法三级联动,只能两级连动。
- 我在CSDN的博客正式开张了!
- 在ASP.NET中实现多文件上传
- 刚刚开始我的程序生涯……
- 自己如何制作RPM包
- patTemplate模板类的使用实例
- access与SqlServer 之时间与日期及其它SQL语句比较
- 在 Solaris 下建立可使用 pkgadd 的软体 package