关于C#读取INI文件出现的问题

mrshu1985 2008-12-13 11:10:17
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Data.SqlClient;
using System.Data;
namespace dblink
{
public class Class1
{

[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, string filepath)
{
WritePrivateProfileString(Section, Key, Value, filepath);
}
//对ini文件进行读操作的函数
public string IniReadValue(string Section, string Key, string filepath)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "", temp,
255, filepath);
return temp.ToString();

}

/*
db =IniReadValue("Database","Database",@"d:\config.ini");
userid=IniReadValue("Database","Userid",@"d:\config.ini");
pwd=IniReadValue("Database","Dbpass",@"d:\config.ini");

*/这部分有错误!!

}


}
注释部分有问题!
请各位大侠帮忙看一下!!!
...全文
145 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
xlfancy 2008-12-13
  • 打赏
  • 举报
回复
一、INI文件读取专用


public class IniFile
{
/// <summary>
/// 读取INI文件专用类
/// </summary>
private Hashtable iniFile = new Hashtable();

public int Count { get { return iniFile.Count; } }
public string this[string IndexKey] { get { return iniFile[IndexKey].ToString(); } }

/// <summary>
/// 读取指定INI文件中的配置信息
/// </summary>
/// <param name="file">配置文件的完整路径名</param>
/// <param name="section">配置文件中的节名</param>
public IniFile(string file, string section)
{
string Section = "[" + section + "]";
LoadIniFile(file, Section);

//如果SpecialIniFilePath不为空,则从SpecialIniFilePath指定的文件中读取配置信息
if (iniFile.Count>0 && iniFile.Contains("SpecialIniFilePath"))
{
string path = this["SpecialIniFilePath"].Trim();
if (path != "") LoadIniFile(path, Section);
}
}

private void LoadIniFile(string filePath, string Section)
{
try
{
StreamReader sr = new StreamReader(filePath, System.Text.Encoding.Default);
string readLine = null;
bool readEnd = false;
string[] keyWord;

while ((readLine = sr.ReadLine()) != null)
{
if (readLine == Section) //是指定的节,则开始读取配置信息
{
while ((readLine = sr.ReadLine()) != null)
{
if (readLine != "") //跳过空行
{
if (readLine.Substring(0, 1) == "[") //是另一新节,则结束本次的读取
{
readEnd = true;
break;
}

keyWord = readLine.Split('=');
iniFile[keyWord[0].Trim()] = keyWord[1];
}
}
}

if (readEnd == true) break;
}
sr.Close();
}
catch
{
iniFile.Clear();
}
}
}



二、INI文件样例

[DBA]
SpecialIniFilePath=

ServerName=kcndgk01
DatabaseName=fhkapp
UserID=07AIQY6Vow5xph2075bjrz7uPX4WOG7767CKS08Tqy3vnf5013dlt19sRZ2UME67
UserPassword=066EMU2Zs91tld929E3fnv3yT80SKC67667GOW4Xu7zrjb49653hpx5wV6YQIA77
Timeout=90

三、使用方法


IniFile ini = new IniFile(System.Windows.Forms.Application.StartupPath + @"\Appcfg.ini", "Appcfg");
if (ini.Count>0)
{
_server = ini["Server"];
_database = ini["Database"];
_uid = ini["UID"];
_uid = Crypt.RndDecrypt("uid", _uid); //解密
_pwd = ini["PWD"];
_pwd = Crypt.RndDecrypt("pwd", _pwd); //解密
_timeout = ini["Timeout"];
}
rometw 2008-12-13
  • 打赏
  • 举报
回复
收藏了
lgamoy 2008-12-13
  • 打赏
  • 举报
回复
提供一个可用的方法。
/// <summary>
/// 读写INI文件
/// </summary>
public class Ini
{
private string fullFileName; //INI文件的完整文件名

[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 Ini(string fullFileName)
{
this.fullFileName = fullFileName;
}

public void Write(string section, string key, string val)
{
WritePrivateProfileString(section, key, val, this.fullFileName);
}

public string Read(string Section,string Key)
{
StringBuilder sb = new StringBuilder(1024);
Ini.GetPrivateProfileString(Section, Key, "", sb, 1024, this.fullFileName);

return sb.ToString();
}
}

如此引用:
Ini ini = new Ini(iniFilePath);
ini.Read("Database", "Database");
lgamoy 2008-12-13
  • 打赏
  • 举报
回复
不知道你的注释的代码是在哪里写着的。方法应该是没错,可能就是静态函数的问题。
猿敲月下码 2008-12-13
  • 打赏
  • 举报
回复
刚测试了下 没什么问题啊:
using System; 
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Data.SqlClient;
using System.Data;
public class class1
{
[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, string filepath)
{
WritePrivateProfileString(Section, Key, Value, filepath);
}
//对ini文件进行读操作的函数
public string IniReadValue(string Section, string Key, string filepath)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "", temp,
255, filepath);
return temp.ToString();
}

}
class test
{
static void Main()
{
string db;
class1 c=new class1();
c.IniWriteValue("Database","Database","bbbbb",@"d:\config.ini");
db=c.IniReadValue("Database","Database",@"d:\config.ini");
Console.WriteLine(db);//我这里输出结果是bbbbb,对的 不知道你错哪了
// db =IniReadValue("Database","Database",@"d:\config.ini");
// Console.WriteLine(db);
// userid=IniReadValue("Database","Userid",@"d:\config.ini");
// pwd=IniReadValue("Database","Dbpass",@"d:\config.ini");
}
}
猿敲月下码 2008-12-13
  • 打赏
  • 举报
回复
int i = GetPrivateProfileString(Section, Key, "", temp, 
255, filepath);
??为什么要用个int i 呢?
sunshine_anycall 2008-12-13
  • 打赏
  • 举报
回复
mark
up
猿敲月下码 2008-12-13
  • 打赏
  • 举报
回复
楼主给你个读取INI的API
[DllImport("kernel32")]//读取INI文件
private static extern long GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

111,130

社区成员

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

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

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