C#为何读不出ini文件的值?

foxd 2010-05-11 10:51:12
用vs 2005 c# 开发winform程序。

以下是代码片段:
using System.Runtime.InteropServices;
class DBClass
{
public class OperateIniFile
{
#region API函数声明

[DllImport("kernel32")]//返回0表示失败,非0为成功
private static extern long WritePrivateProfileString(string section,string key,
string val,string filePath);

[DllImport("kernel32")]//返回取得字符串缓冲区的长度
private static extern long GetPrivateProfileString(string section,string key,
string def,StringBuilder retVal,int size,string filePath);
#endregion

#region 读Ini文件
public static string ReadIniData(string Section,string Key,string NoText,string iniFilePath)
{
if(File.Exists(iniFilePath))
{
StringBuilder temp = new StringBuilder(1024);
GetPrivateProfileString(Section,Key,NoText,temp,1024,iniFilePath);
return temp.ToString(); //在这里设断点,运行后发现temp的值为空
}
else
{
return String.Empty;
}
}
#endregion
}

#region 建立数据库连接
/// <summary>
/// 建立数据库连接.
/// </summary>
/// <returns>返回SqlConnection对象</returns>
public SqlConnection getcon()
{
Weighing.BaseClass.DBClass.OperateIniFile inif = new Weighing.BaseClass.DBClass.OperateIniFile();
string server = DBClass.OperateIniFile.ReadIniData("Server", "Name", "(Local)", "Config.ini");
if (server.Length == 0)
{
MessageBox.Show("配置文件(Config.ini)缺失!");
Application.Exit();
}
//MSSQL
//string M_str_sqlcon = "Data Source=" + server + ";Database=db_Weighing;User id=sa;PWD=";

//GSQL
string M_str_sqlcon = "Data Source=" + server + ";Database=db_Weighing;User id=sa;PWD=admin";

SqlConnection myCon = new SqlConnection(M_str_sqlcon);
return myCon;
}
#endregion

我的配置文件Config.ini放在当前项目的bin\debug下面,其内容为:
; 服务器名填在等号右边
[Server]
Name=YSFRI-DVUBVSWDB\\GSQL

运行后发现,无论Config.ini中的Name的值是什么,字符串server的值总是为(Local),说明取不出值来,Why? 请教各位!
...全文
323 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
l_courser 2010-08-25
  • 打赏
  • 举报
回复
我也出现了第一行不能有数据,如果第一行是[***]则该片段的数据无法读取
deknight 2010-05-12
  • 打赏
  • 举报
回复
mark
cnh3303 2010-05-12
  • 打赏
  • 举报
回复
看看文件路径对不对,同6楼
tom9812 2010-05-12
  • 打赏
  • 举报
回复
今天我刚好用的读取ini ,遇到的这个问题,顺便找到了问题所在,同仁们可以测试一下
tom9812 2010-05-12
  • 打赏
  • 举报
回复
GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
用这个读取ini时,ini的路径不要有重名的文件夹 例如 c:\aa\bb\conf.ini 读取没有问题
但c:\aa\aa\conf.ini 或 c:\aa\bb\aa\conf.ini 等就无法读取成功了
raon0414 2010-05-11
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 foxd 的回复:]
to raon0414:
用了你的代码也读不出来,Why?
[/Quote]
你确认ini文件路径是正确的?
andybang1981 2010-05-11
  • 打赏
  • 举报
回复
使用如下API,保证测试通过,INI文件如下:
[Master]
PrtPort=LPT1:
Delay=100
Qty1=0
Qty2=0
Qty3=0

[DllImport("kernel32.dll", EntryPoint = "GetPrivateProfileString")]
public static extern int GetPrivateProfileString(
string lpApplicationName,
string lpKeyName,
string lpDefault,
StringBuilder lpReturnedString,
int nSize,
string lpFileName
);

[DllImport("kernel32.dll", EntryPoint = "WritePrivateProfileString")]
public static extern int WritePrivateProfileString(
string lpApplicationName,
string lpKeyName,
string lpString,
string lpFileName
);
调用如下:
public static void SetIniFile(string strSection, string strNode, string strValue)
{
clsMain.WritePrivateProfileString(strSection, strNode, strValue, Application.StartupPath + @"\iniLabel.ini");
}

public static string GetIniFile(string strSection, string strNode)
{
StringBuilder strResult = new StringBuilder(1024);

clsMain.GetPrivateProfileString(strSection, strNode, "", strResult, 1024, Application.StartupPath + @"\iniLabel.ini");
return strResult.ToString();
}
Carpathia 2010-05-11
  • 打赏
  • 举报
回复
两种方式你看看
1.第一行设置一个;号,你将配置文件更改为
;
[Server]
Name=YSFRI-DVUBVSWDB\\GSQL


2.更改下INI文件的编码方式,utf8或者ascii都试一下
foxd 2010-05-11
  • 打赏
  • 举报
回复
to raon0414:
用了你的代码也读不出来,Why?
raon0414 2010-05-11
  • 打赏
  • 举报
回复
//ini文件操作类
public class INIClass
{
public string inipath;
[DllImport("kernel32")]
private static extern bool 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;
}
/// 写入INI文件

public void IniWriteValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, this.inipath);
}

/// 读出INI文件
public string IniReadValue(string Section, string Key)
{
StringBuilder temp = new StringBuilder(500);
int i = GetPrivateProfileString(Section, Key, "", temp, 500, this.inipath);
return temp.ToString();
}

/// 删除指定区域。
/// <param name= "Section "> 指定区域名。 </param>
/// <returns> 返回删除是否成功。 </returns>
public bool EraseSection(string Section, string FileName)
{
return WritePrivateProfileString(Section, null, null, FileName);
}

/// 验证文件是否存在

/// 布尔值
public bool ExistINIFile()
{
return File.Exists(inipath);
}
}

111,095

社区成员

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

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

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