关于C#读取INI文件出现的问题
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");
*/这部分有错误!!
}
}
注释部分有问题!
请各位大侠帮忙看一下!!! --------------------编程问答-------------------- 不知道你的注释的代码是在哪里写着的。方法应该是没错,可能就是静态函数的问题。 --------------------编程问答-------------------- 提供一个可用的方法。
/// <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"); --------------------编程问答-------------------- 收藏了 --------------------编程问答-------------------- 一、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=k易做图gk01
DatabaseName=fhkapp
UserID=07AIQY6Vow5xph2075bjrz7uPX4WOG7767CKS08Tqy3vnf5013dlt19sRZ2UME67
UserPassword=066EMU2Zs91tld929E3fnv3yT80SKC67667GOW4Xu7zrjb49653hpx5wV6YQIA77
Timeout=90
三、使用方法
--------------------编程问答-------------------- 楼主给你个读取INI的API
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"];
}
[DllImport("kernel32")]//读取INI文件--------------------编程问答-------------------- mark
private static extern long GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
up --------------------编程问答--------------------
int i = GetPrivateProfileString(Section, Key, "", temp,??为什么要用个int i 呢? --------------------编程问答-------------------- 刚测试了下 没什么问题啊:
255, filepath);
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");
}
}
补充:.NET技术 , C#