如何读取到.ini文件中内容

taotao1130 2004-03-22 04:11:12
如何读取到.ini文件中内容,文件中有中文又该如何处理??
...全文
112 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
taotao1130 2004-03-22
  • 打赏
  • 举报
回复
我测试过了,可以用!
我只是想多了解一些!
delphizd 2004-03-22
  • 打赏
  • 举报
回复
test!
taotao1130 2004-03-22
  • 打赏
  • 举报
回复
int i = GetPrivateProfileString(Section,Key,"",temp,255,this.Path);
这里的参数可以给解释一下吗?还有从那里可以知道这些API?
谢谢
northwest_wind 2004-03-22
  • 打赏
  • 举报
回复
把下面的代码改动一下,就可以在你的程序中使用,当然
别忘记加上名字空间哦。
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;
}

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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