我是一个初学者,谁有 c# winform 中用到ini 配置文件的具体例子,发我一份紧用作学习之用,谢谢~

比如能用ini 文件设置字体,大小等一些属性,在线等~~
2024-12-12 01:23:32
推荐回答(4个)
回答1:

你得先下载一个kernel32.dll然后在你的项目中添加引用
下面是我做过的一个例子,你可以参考一下
public class inifile
{
#region 导入dll函数
[DllImport("kernel32.dll")]
public extern static int GetPrivateProfileIntA(string segname, string keyname, int idefault, string filename);
[DllImport("kernel32.dll")]
public extern static int GetPrivateProfileStringA(string segname, string keyname, string sdefault, StringBuilder retvalue, int nsize, string filename);
[DllImport("kernel32.dll")]
public extern static int GetPrivateProfileSectionA(string segname, byte[] sdata, int nsize, string filename);
[DllImport("kernel32.dll")]
public extern static int WritePrivateProfileSectionA(string segname, byte[] sdata, string filename);
[DllImport("kernel32.dll")]
public extern static int WritePrivateProfileStringA(string segname, string keyname, string svalue, string filename);
[DllImport("kernel32.dll")]
public extern static int GetPrivateProfileSectionNamesA(byte[] vdata, int ilen, string filename);
#endregion
private string _path;
///


/// 所有的设置节
///

public inisegments segments;
///
/// 构造函数
///

/// ini文件路径
public inifile(string vpath)
{
_path = vpath;
segments = new inisegments(this);
byte[] bufsegs = new byte[32767];

int rel = GetPrivateProfileSectionNamesA(bufsegs, 32767, _path);
int icnt, ipos;
string tmp;
if (rel > 0)
{
icnt = 0; ipos = 0;
for (icnt = 0; icnt < rel; icnt++)
{
if (bufsegs[icnt] == 0x00)
{
tmp = System.Text.ASCIIEncoding.Default.GetString(bufsegs, ipos, icnt).Trim();
ipos = icnt + 1;
if (tmp != "")
segments.add(tmp);
}
}
}
}
///
/// 获取ini文件路径
///

public string path
{
get { return _path; }
}
///
/// 读取一个整数型的设置值
///

/// 设置节名
/// 设置项名
/// 默认值
/// 设置值
public int getint(string segname, string keyname, int idefault)
{
return GetPrivateProfileIntA(segname, keyname, idefault, _path);
}

///
/// 读取一个字符串型设置值
///

/// 设置节名
/// 设置项名
/// 默认值
/// 设置值
public string getstring(string segname, string keyname, string sdefault)
{
StringBuilder red = new StringBuilder(1024);
GetPrivateProfileStringA(segname, keyname, "", red, 1024, _path);
return red.ToString();
}

///
/// 写入设置项
///

/// 设置节名
/// 设置项名
/// 设置值
public void setstring(string segname, string keyname, string vvalue)
{
WritePrivateProfileStringA(segname, keyname, vvalue, _path);
}
///
/// 写入一个设置节
///

/// 设置节名
/// 数据
///
/// 数据为多个设置项组成的字符串,每个设置项之间用 "\0" 分割
/// 字符串最后用 "\0\0" 结束
///

///
/// writesegment(segname,"\0\0"); 能用于清除一个设置节下的所有设置项
///

public void writesegment(string segname, string vdata)
{
WritePrivateProfileSectionA(segname, System.Text.ASCIIEncoding.Default.GetBytes(vdata), _path);
}
///
/// 读取一个设置节下面的所有设置项
///

/// 要读取的设置节
public void getsegment(inisegment o)
{
byte[] vdata = new byte[32767];
int rlen = GetPrivateProfileSectionA(o.name, vdata, 32767, _path);
o.items.Clear();
if (rlen < 1) return;
string tmp = "";
int ipos, icnt;
ipos = 0;
for (icnt = 0; icnt < rlen; icnt++)
{
if (vdata[icnt] == 0x00)
{
tmp = System.Text.ASCIIEncoding.Default.GetString(vdata, ipos, icnt - ipos).Trim();
if (tmp != "")
{
string[] t = tmp.Split('=');
if (t.Length <= 1)
o.items.add(t[0], "");
else
o.items.add(t[0], t[1]);
}
ipos = icnt + 1;
}
}
}
}
用的时候就直接实例化这个写好的方法类 进行编辑
string COM = new inifile("ini文件地址").segments["分类名称"].items["子项属性"].value
这样就可以获取。
例如:
人(分类名称)
身高(子项属性)=100
体重(子项属性)=55
inifile ini = new inifile("ini文件路径");
ini.setstring("需要修改的分类名", "子项", "值");

回答2:

我记得有专门操作ini文件的类,我一以前有这样一个DLL文件,去网上搜下吧。

回答3:

自己去下载一个啊

回答4:

写一个ini的类:
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.InteropServices;
using System.IO;
using System.Text;
namespace Client
{
public class Ini
{
private static string _FileName; //INI文件名
public static string FileName
{
get
{
return _FileName;
}
set
{
_FileName = value;
}
}
//声明读写INI文件的API函数
[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, byte[] retVal, int size, string filePath);
//写INI文件
public static void WriteString(string Section, string Ident, string Value)
{
if (!WritePrivateProfileString(Section, Ident, Value, _FileName))
{

throw (new ApplicationException("写Ini文件出错"));
}
}
//读取INI文件指定
public static string ReadString(string Section, string Ident, string Default)
{
Byte[] Buffer = new Byte[200];
int bufLen = GetPrivateProfileString(Section, Ident, Default, Buffer, Buffer.GetUpperBound(0), _FileName);
//必须设定0(系统默认的代码页)的编码方式,否则无法支持中文
string s = Encoding.GetEncoding(0).GetString(Buffer);
s = s.Substring(0, bufLen);
s = s.Replace("\0", "");
return s.Trim();
}

//读整数
public static int ReadInteger(string Section, string Ident, int Default)
{
string intStr = ReadString(Section, Ident, Convert.ToString(Default));
try
{
return Convert.ToInt32(intStr);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return Default;
}
}
//写整数
public static void WriteInteger(string Section, string Ident, int Value)
{
WriteString(Section, Ident, Value.ToString());
}
//读布尔
public static bool ReadBool(string Section, string Ident, bool Default)
{
try
{
return Convert.ToBoolean(ReadString(Section, Ident, Convert.ToString(Default)));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return Default;
}
}

//写Bool
public static void WriteBool(string Section, string Ident, bool Value)
{
WriteString(Section, Ident, Convert.ToString(Value));
}

//从Ini文件中,将指定的Section名称中的所有Ident添加到列表中
public static void ReadSection(string Section, StringCollection Idents)
{
Byte[] Buffer = new Byte[16384 * 2];
//Idents.Clear();

int bufLen = GetPrivateProfileString(Section, null, null, Buffer, Buffer.GetUpperBound(0),
FileName);
//对Section进行解析
GetStringsFromBuffer(Buffer, bufLen, Idents);
}

private static void GetStringsFromBuffer(Byte[] Buffer, int bufLen, StringCollection Strings)
{
Strings.Clear();
if (bufLen != 0)
{
int start = 0;
for (int i = 0; i < bufLen; i++)
{
if ((Buffer[i] == 0) && ((i - start) > 0))
{
String s = Encoding.GetEncoding(0).GetString(Buffer, start, i - start);
Strings.Add(s);
start = i + 1;
}
}
}
}
//从Ini文件中,读取所有的Sections的名称
public static void ReadSections(StringCollection SectionList)
{
//Note:必须得用Bytes来实现,StringBuilder只能取到第一个Section
byte[] Buffer = new byte[65535];
int bufLen = 0;
bufLen = GetPrivateProfileString(null, null, null, Buffer,
Buffer.GetUpperBound(0), _FileName);
GetStringsFromBuffer(Buffer, bufLen, SectionList);
}
//读取指定的Section的所有Value到列表中
public static void ReadSectionValues(string Section, NameValueCollection Values)
{
StringCollection KeyList = new StringCollection();
ReadSection(Section, KeyList);
Values.Clear();
foreach (string key in KeyList)
{
Values.Add(key, ReadString(Section, key, ""));

}
}
/**/
////读取指定的Section的所有Value到列表中,
//public void ReadSectionValues(string Section, NameValueCollection Values,char splitString)
//{ string sectionValue;
// string[] sectionValueSplit;
// StringCollection KeyList = new StringCollection();
// ReadSection(Section, KeyList);
// Values.Clear();
// foreach (string key in KeyList)
// {
// sectionValue=ReadString(Section, key, "");
// sectionValueSplit=sectionValue.Split(splitString);
// Values.Add(key, sectionValueSplit[0].ToString(),sectionValueSplit[1].ToString());

// }
//}
//清除某个Section
public static void EraseSection(string Section)
{
//
if (!WritePrivateProfileString(Section, null, null, _FileName))
{
throw (new ApplicationException("无法清除Ini文件中的Section"));
}
}
//删除某个Section下的键
public static void DeleteKey(string Section, string Ident)
{
WritePrivateProfileString(Section, Ident, null, _FileName);
}
//Note:对于Win9X,来说需要实现UpdateFile方法将缓冲中的数据写入文件
//在Win NT, 2000和XP上,都是直接写文件,没有缓冲,所以,无须实现UpdateFile
//执行完对Ini文件的修改之后,应该调用本方法更新缓冲区。
public static void UpdateFile()
{
WritePrivateProfileString(null, null, null, _FileName);
}

//检查某个Section下的某个键值是否存在
public static bool ValueExists(string Section, string Ident)
{
//
StringCollection Idents = new StringCollection();
ReadSection(Section, Idents);
return Idents.IndexOf(Ident) > -1;
}

//确保资源的释放
}

}

读INI文件:Ini.FileName = Application.StartupPath + "\\aaaa.ini";
要用什么方法可直接调用