c#怎么读取ini文件中的内容

Anders_lt 2006-04-20 02:23:24
有没有具体的方法将 ini中的所有session 读取到一个集合中
...全文
872 22 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
smysky 2006-12-12
  • 打赏
  • 举报
回复
mark
yanwc 2006-10-19
  • 打赏
  • 举报
回复
mark
ap0106204 2006-10-19
  • 打赏
  • 举报
回复
保存
zhlovers 2006-04-21
  • 打赏
  • 举报
回复
收藏!
wangyu2481 2006-04-21
  • 打赏
  • 举报
回复
收藏
kissknife 2006-04-20
  • 打赏
  • 举报
回复
提示类或命名空间不存在

==============================

using System.Runtime.InteropServices;
zhsleep 2006-04-20
  • 打赏
  • 举报
回复
比如说一个存邮件地址的ini文件如下:[sent]
str1=xxx@163.com
[accept]
str2=kkk@163.com

可以在程序里如下写:

using System.Text;
using System.Runtime.InteropServices;

在类里:
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section,string key,string def,StringBuilder retVal,int size,string filePath);

在发送邮件的事件里面就可以获得邮件地址:
StringBuilder temp1 = new StringBuilder ( 255 ) ;
GetPrivateProfileString( "sent" , "str1" ,"aaa!", temp1 , 255 , filepath+"mail.ini" ) ;
string fromAddr=temp1.ToString();

StringBuilder temp2 = new StringBuilder ( 255 ) ;
GetPrivateProfileString( "accept" , "str2" ,"aaa!", temp2 , 255 , filepath+"mail.ini" ) ;
string toAddr=temp2.ToString();

当然,可以写一个读取ini文件的类。
Anders_lt 2006-04-20
  • 打赏
  • 举报
回复
我用这两个了,提示没有命名空间
jingtao_zhou 2006-04-20
  • 打赏
  • 举报
回复
用这2个函数就可以了
[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);
Anders_lt 2006-04-20
  • 打赏
  • 举报
回复
怎么应用 ,我的vs2003是刚装的
liujiwe79 2006-04-20
  • 打赏
  • 举报
回复
应用一下那个dll就行了
-渔民- 2006-04-20
  • 打赏
  • 举报
回复
mark
Anders_lt 2006-04-20
  • 打赏
  • 举报
回复
为什么我编译 提示类或者命名空间不存在呢
活靶子哥哥 2006-04-20
  • 打赏
  • 举报
回复
http://www.aspxboy.com/private/showthread.asp?threadid=388
copico 2006-04-20
  • 打赏
  • 举报
回复
是system32里面的
jingtao_zhou 2006-04-20
  • 打赏
  • 举报
回复
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace MyClock
{
/// <summary>
/// Ini 的摘要说明。
/// </summary>
public class Ini
{

public string path; //INI文件名


public Ini(string INIPath)
{
//
// TODO: 在此处添加构造函数逻辑
//
path = 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);

//声明读写INI文件的API函数




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();

}

//读取INI文件指定
}
}
real3000 2006-04-20
  • 打赏
  • 举报
回复
关注
Anders_lt 2006-04-20
  • 打赏
  • 举报
回复
[DllImport("kernel32")]

这个什么意思? kernel32 这个是系统中有的吗
copico 2006-04-20
  • 打赏
  • 举报
回复
http://www.xmlasp.net/n1661c13.aspx
copico 2006-04-20
  • 打赏
  • 举报
回复

把下面的代码改动一下,就可以在你的程序中使用,当然
别忘记加上名字空间哦。
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;
}
加载更多回复(2)

111,098

社区成员

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

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

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