| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1475 人关注过本帖
标题:如何读取INI文件的内容
只看楼主 加入收藏
hxhfg
Rank: 1
等 级:新手上路
帖 子:284
专家分:0
注 册:2006-4-18
收藏
 问题点数:0 回复次数:7 
如何读取INI文件的内容
我的INI文件如下:
[login]
Provider=SQLOLEDB.1
Password=123
Persist Security Info=True
User ID=user
Initial Catalog=ken
Data Source=mydata

如何读取INI文件的内容 ??
搜索更多相关主题的帖子: INI文件 Catalog Provider Source Security 
2007-07-20 10:15
hxhfg
Rank: 1
等 级:新手上路
帖 子:284
专家分:0
注 册:2006-4-18
收藏
得分:0 

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;
}


这样试了,不行,大家帮看一下


2007-07-20 11:26
hxhfg
Rank: 1
等 级:新手上路
帖 子:284
专家分:0
注 册:2006-4-18
收藏
得分:0 
自己顶一下

2007-07-21 12:46
bygg
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:乖乖的心中
等 级:版主
威 望:241
帖 子:13555
专家分:3076
注 册:2006-10-23
收藏
得分:0 
你设置个断点看一下.,..

飘过~~
2007-07-21 16:17
hxhfg
Rank: 1
等 级:新手上路
帖 子:284
专家分:0
注 册:2006-4-18
收藏
得分:0 
什么设置断点??

2007-07-22 23:31
狼度
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2007-7-20
收藏
得分:0 

在你感觉有问题的代码上按'F9'出现的红点,那就是断点了
下班给你找找,我这应该有这方面的代码


己所不欲 勿施于人
2007-07-23 09:31
hxhfg
Rank: 1
等 级:新手上路
帖 子:284
专家分:0
注 册:2006-4-18
收藏
得分:0 
狼度,兄弟,你代码给我好吗?

2007-07-23 11:00
快速回复:如何读取INI文件的内容
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019238 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved