ini文件里的信息读不出来,郁闷死了

xuexiziji 2009-09-16 06:25:59
以下是读ini文件的类


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 string GetPath()
{
string strpath = System.Windows.Forms.Application.StartupPath+"\\out.ini";
return strpath;
}


public static string MyDatainfo()
{
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 =" + sPwd + ";database =" + DataName;
return strSql;
}


ini文件里就有
[Database]
server=localhost
database=outlook
uid=root
pwd=''

点破东西

结果MyDatainfo()
里那四个变量都是空的
咋搞的呢?
...全文
340 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
waz99 2009-09-18
  • 打赏
  • 举报
回复
谢谢大家的回复,这是我的另一个号,因为这个号没分了借了个号问问题

读不出来的原因是因为我的路径中有汉语存在
路径中有日语和英语没问题了,可以读出来了,可能因为我的系统是日语的原因

谢谢大家了
xuexiziji 2009-09-17
  • 打赏
  • 举报
回复
开始是我用txt文件修改的,我发现文件是out.ini.txt这样的,我读的时候判断文件不存在,修改后判断文件是存在了,但是读出来的信息还是空的,不知道怎么回事
xuexiziji 2009-09-17
  • 打赏
  • 举报
回复
存在的话读取还是错误的,那就有问题了。
我这里可以了。
xuexiziji 2009-09-17
  • 打赏
  • 举报
回复
在类里加上一个方法。
/// <summary>
/// 验证文件是否存在
/// </summary>
/// <returns>布尔值</returns>
public bool ExistINIFile()
{
return File.Exists(inipath);
}

然后在这里使用,看看文件存在吗。
public static string MyDatainfo()
{
string sPath;
string ServerName, userId, sPwd, DataName;

sPath = GetPath();
IniFile ini = new IniFile(sPath);
if (ini.ExistINIFile())
{
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 =" + sPwd + ";database =" + DataName;
return strSql;
}
}

limeng315 2009-09-17
  • 打赏
  • 举报
回复
强烈建议楼主用xml格式存储,通过序列化的方式读取。具体如下
声明一个你的数据库信息的类
public class MyDatabaseInfo
{
public string server;
public string database;
public string uid;
public string pwd;

}

在程序中声明一个实例
MyDatabaseInfo dbInfo = new MyDatabaseInfo();
dbInfo.server = "....";
dbInfo.database= "....";
dbInfo.uid = "....";
dbInfo.pwd = "....";

//把对象dbInfo保存到xml中
public void SaveToXml(string path, MyDatabaseInfo dbInfo)
{
XmlSerializer xs = new XmlSerializer(typeof(MyDatabaseInfo));
FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite);
xs.Serialize(fs, dbInfo);
fs.Close();
}
//把对象从xml中读出来
public MyDatabaseInfo ReadFromXml(string path)
{
XmlSerializer xs = new XmlSerializer(typeof(MyDatabaseInfo));
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
MyDatabaseInfo dbInfo = (MyDatabaseInfo)xs.Deserialize(fs);
fs.Close();
return dbInfo;
}

具体的根据需要你在整理下。
xuexiziji 2009-09-17
  • 打赏
  • 举报
回复
谢谢大家的回复,其实我用ini就是因为生成可执行安装文件在公司另一个地方可以用,那儿的数据库用户名和密码等不知道是什么,为了方便修改才用ini的,有什么别的办法能解决这个问题也可以

lzsh0622 2009-09-17
  • 打赏
  • 举报
回复
楼主再试试,把这个类改成静态类,不要用实例化的方法。调试时感觉是递归调用的执行路线,很费神的。
xuexiziji 2009-09-16
  • 打赏
  • 举报
回复
和大家给的代码差不多,没有什么特别的地方。为什么不能读取呢。
lzsh0622 2009-09-16
  • 打赏
  • 举报
回复
从逻辑上没找出错,有一种可能:楼主,仔细查一下,INI文件是保存在这个位置下吗?
 很可能不是啊.

解决读写INI文件,写到静态类中,不用实例化,一层方法就能解决问题。
这样比较简洁,可读性、可维护要好。


public static class IniFile
{
//声明读写INI文件的API函数
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
[DllImport("kernel32")]
public static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

static string path = Application.StartupPath + "\\out1.ini";
//读 INI
public static string GetSqlString()
{
if (System.IO.File.Exists(path) == false) return "";

StringBuilder temp1 = new StringBuilder(255);
StringBuilder temp2 = new StringBuilder(255);
StringBuilder temp3 = new StringBuilder(255);
StringBuilder temp4 = new StringBuilder(255);

int i1 = GetPrivateProfileString("Database", "server", "", temp1, 255, path);
int i2 = GetPrivateProfileString("Database", "uid", "", temp2, 255, path);
int i3 = GetPrivateProfileString("Database", "pwd", "", temp3, 255, path);
int i4 = GetPrivateProfileString("Database", "database", "", temp4, 255, path);

StringBuilder strSql = new StringBuilder(255);
strSql.Append("server =" + temp1.ToString().Trim());
strSql.Append(";uid = " + temp2.ToString().Trim());
strSql.Append(";pwd =" + temp3.ToString().Trim());
strSql.Append(";database =" + temp4.ToString().Trim());

return strSql.ToString();
}

// 写 INI
public static void WriteIni(string server, string uid, string pwd, string database)
{
WritePrivateProfileString("Database", "server", server, path);
WritePrivateProfileString("Database", "uid", uid, path);
WritePrivateProfileString("Database", "pwd", pwd, path);
WritePrivateProfileString("Database", "database", database, path);
}
}
wuyq11 2009-09-16
  • 打赏
  • 举报
回复
public string inipath;
[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 INIClass(string INIPath)
{
inipath = INIPath;
}
public void IniWriteValue(string Section,string Key,string Value)
{
WritePrivateProfileString(Section,Key,Value,this.inipath);
}
public string IniReadValue(string Section,string Key)
{
StringBuilder temp = new StringBuilder(500);
int i = GetPrivateProfileString(Section,Key,"",temp,500,this.inipath);
return temp.ToString();
}
public bool ExistINIFile()
{
return File.Exists(inipath);
}
http://topic.csdn.net/u/20090406/12/90712efe-bd44-49bc-9a2a-b0d1790cbb6c.html
zylsky 2009-09-16
  • 打赏
  • 举报
回复
先做一个写ini文件的,用程序生成一个ini文件,然后再去读。不要自己弄。
xuexiziji 2009-09-16
  • 打赏
  • 举报
回复
用xml也可以。我就是不明白为什么不能读取呢。没看出来那里错了。
我 把 255 改成 1024试一下
StringBuilder temp = new StringBuilder(1024);
int i = GetPrivateProfileString(Section,Key,"error",temp,1024,this.Path);
Error_Code 2009-09-16
  • 打赏
  • 举报
回复
既然都C#了 为何还ini 弃xml于不顾?
xuexiziji 2009-09-16
  • 打赏
  • 举报
回复
我也是从网上找的,不是很明白,唉。。。
xuexiziji 2009-09-16
  • 打赏
  • 举报
回复
配置文件怎么用的。
鸭梨山大帝 2009-09-16
  • 打赏
  • 举报
回复
[DllImport("kernel32")]

private static extern int GetPrivateProfileString(string section,string key,string def,StringBuilder retVal,int size,string filePath);

StringBuilder retVal <-- StringBuilder ? 没见过这用法
美人心计1999 2009-09-16
  • 打赏
  • 举报
回复
c#里面有配置文件,
空白是我 2009-09-16
  • 打赏
  • 举报
回复
试着 写一个键值 再读取 看有问题吗?
王向飞 2009-09-16
  • 打赏
  • 举报
回复
好方法先写再读
xuexiziji 2009-09-16
  • 打赏
  • 举报
回复
先做一个写ini文件的,用程序生成一个ini文件,然后再去读。不要自己弄。
加载更多回复(2)

110,571

社区成员

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

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

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