C#中如何读写ini文件?????

qufusongyu 2004-07-09 01:43:32
我想在ini文件中放入一些连接的信息,如密码,用户,还有服务器名,如何在
C#中如何读写ini文件?????
...全文
455 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
白水山路 2005-04-09
  • 打赏
  • 举报
回复
晕,可以了
白水山路 2005-04-09
  • 打赏
  • 举报
回复
TO stdotleo(大雨仔|NJSIA.PA筹委会)
写入的成功,但我的读取出现错误了!怎么解决
deyangwangyu 2005-03-15
  • 打赏
  • 举报
回复
up
feihu_02 2005-03-14
  • 打赏
  • 举报
回复
up
qufusongyu 2004-07-10
  • 打赏
  • 举报
回复
谢谢。
大雨仔 2004-07-09
  • 打赏
  • 举报
回复
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;


namespace Sx_Mdi
{

/// <summary>
/// Summary description 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;
}


110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧