• 全部
  • C#综合技术
  • C#互联网桌面应用
  • AppLauncher
  • WinForm&WPF
  • C#开发新技术
  • 问答

请给一个读取.ini配置文件的实例,谢谢!

sun_changzhan 2004-10-26 04:13:20
请给一个读取.ini配置文件的实例,谢谢!
...全文
193 点赞 收藏 5
写回复
5 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
chenszhs 2004-10-27
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace Chensz.IniFiles
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
///
public interface IIniFiles
{
/// <summary>
/// 读取int类型的数据
/// </summary>
/// <param name="filename">文件名</param>
/// <param name="section">组名</param>
/// <param name="key">键名</param>
/// <param name="Default">默认值</param>
/// <returns></returns>
int profileInt ( string filename, string section, string key, int Default );
/// <summary>
/// 读取string类型的数据
/// </summary>
/// <param name="filename">文件名</param>
/// <param name="section">组名</param>
/// <param name="key">键名</param>
/// <param name="Default">默认值</param>
/// <returns></returns>
string profileString ( string filename, string section, string key, string Default );
/// <summary>
/// 设置值
/// </summary>
/// <param name="filename">文件名</param>
/// <param name="section">组名</param>
/// <param name="key">键名</param>
/// <param name="Value">值</param>
/// <returns></returns>
int setProfileString ( string filename,string section,string key,string Value );
}
/// <summary>
/// Ini操作类
/// </summary>
public class IniFiles:IIniFiles
{
/// <summary>
///
/// </summary>
public IniFiles()
{
}

#region IIniFiles 成员


[DllImport("kernel32.dll", EntryPoint="GetPrivateProfileIntA")]
private static extern int ProfileInt(string lpAppName,string lpKeyName,int nDefault,string lpFileName);

int IIniFiles.profileInt(string filename, string section, string key, int Default)
{
try
{
return ProfileInt(section,key,Default,filename);
}
catch
{
return Default;
}
}


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

string IIniFiles.profileString(string filename, string section, string key, string Default)
{
try
{
StringBuilder s=new StringBuilder(255);
ProfileString(section,key,Default,s,255,filename);
string sReturn=s.ToString();
return sReturn;
}
catch
{
return Default;
}
}


[DllImport("kernel32.dll", EntryPoint="WritePrivateProfileStringA")]
private static extern int SetProfileString(string lpAppName,string lpKeyName,string nDefault,string lpFileName);

int IIniFiles.setProfileString(string filename, string section, string key, string Value)
{
try
{
SetProfileString(section,key,Value,filename);
return 1;
}
catch
{
return -1;
}
}

#endregion
}
}
回复
huguguhu 2004-10-27
string path1=@"filepath+"*.ini"
string enable=string.Empty; StreamReader sr = new StreamReader(path,System.Text.Encoding.Default);
while(sr.ReadLine().ToString()!="")
{
if(i==0)
{
enable=sr.ReadLine().ToString();
break;
}

}
回复
cs920 2004-10-27
up
回复
xj5162004 2004-10-27
the_way(路)
说得不错,向你学习
回复
the_way 2004-10-26
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace IniFile
{
/// <summary>
/// IniFile 的摘要说明。
/// </summary>
public class IniFile
{
private string FFileName;

[DllImport("kernel32")]
private static extern int GetPrivateProfileInt(
string lpAppName,
string lpKeyName,
int nDefault,
string lpFileName
);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(
string lpAppName,
string lpKeyName,
string lpDefault,
StringBuilder lpReturnedString,
int nSize,
string lpFileName
);
[DllImport("kernel32")]
private static extern bool WritePrivateProfileString(
string lpAppName,
string lpKeyName,
string lpString,
string lpFileName
);

public IniFile(string filename)
{
FFileName = filename;
}
public int ReadInt(string section,string key,int def)
{
return GetPrivateProfileInt(section,key,def,FFileName);
}
public string ReadString(string section,string key,string def)
{
StringBuilder temp = new StringBuilder(1024);
GetPrivateProfileString(section,key,def,temp,1024,FFileName);
return temp.ToString();
}
public void WriteInt(string section,string key,int iVal)
{
WritePrivateProfileString(section,key,iVal.ToString(),FFileName);
}
public void WriteString(string section,string key,string strVal)
{
WritePrivateProfileString(section,key,strVal,FFileName);
}
public void DelKey(string section,string key)
{
WritePrivateProfileString(section,key,null,FFileName);
}
public void DelSection(string section)
{
WritePrivateProfileString(section,null,null,FFileName);
}

}
}
回复
相关推荐
发帖
C#
创建于2007-09-28

10.5w+

社区成员

.NET技术 C#
申请成为版主
帖子事件
创建了帖子
2004-10-26 04:13
社区公告

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