C# 操作ini文件

作者&投稿:聊贪 (若有异议请与网页底部的电邮联系)
如何在C#中读写INI文件~

C#操作INI文件使用的是Windows系统自带Win32的API函数——WritePrivateProfileString()和GetPrivateProfileString()函数
参考百度的文库
http://wenku.baidu.com/link?url=gpXS_zu3f4HHxTtN4M38hNrl-0G5YJ_cZDeekkKqhzeXlKir5YfQ7yBZrC87H6Ebkk2rv5V8bb_dl9_5CC5uAKZDVirXGJaPlKbz8tJ8TTO

创建INI文件,创建完成之后在启动文件的目录下
public class IniOP
{
#region 声明读写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);
#endregion

#region 变量声明
//初始化路径名,主键名,字符串长度,这里需要加一个说明,在对应函数才需要做类似操作,初始值应该为空
private string m_strFilePath = "";
private string m_strKey = "";
private string m_strSubKey = "";
private int m_intSize = 255;
private string _strFilePath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\Default.ini";
private string _strKey = "ErrorKeyIsNull";
private string _strSubKey = "ErrorSubKeyIsNull";
#endregion

#region 属性设置
//设置路径属性
public string FilePath { get { return m_strFilePath; } set { m_strFilePath = value; } }
//设置主键名属性
public string Key { get { return m_strKey; } set { m_strKey = value; } }
//设置字符串长度属性
public int Size { get { return m_intSize; } set { m_intSize = value; } }
//设置子键名属性
public string SubKey { get { return m_strSubKey; } set { m_strSubKey = value; } }
//版本信息
public static string Version
{
get
{
return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
}
}
#endregion

#region 构造函数
//构造函数,无参数
public IniOP()
{
}
//构造函数,路径名
public IniOP(string strPath)
{
m_strFilePath = strPath;
}
//构造函数,路径名,主键名
public IniOP(string strPath, string strKey)
{
m_strFilePath = strPath;
m_strKey = strKey;
}
//构造函数,路径名,主键名,字符串长度
public IniOP(string strPath, string strKey, int intSize)
{
m_strFilePath = strPath;
m_strKey = strKey;
m_intSize = intSize;
}
//构造函数,路径名,主键名,字符串长度,子键名
public IniOP(string strPath, string strKey, int intSize, string strSubKey)
{
m_strFilePath = strPath;
m_strKey = strKey;
m_intSize = intSize;
m_strSubKey = strSubKey;
}
#endregion

#region 写入ini文件时的默认值判断
private void IniCheckDefaultValue()
{
if (m_strFilePath == "" || System.IO.File.Exists(m_strFilePath) == false)
{
_strFilePath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\Default.ini";
}
else
{
_strFilePath = m_strFilePath;
}
if (m_strKey == "")
{
_strKey = "ErrorKeyIsNull_" + System.DateTime.Now.ToString("yyyyMMddHHmmss");
}
else
{
_strKey = m_strKey;
}
if (m_strSubKey == "")
{
_strSubKey = "ErrorSubKeyIsNull_" + System.DateTime.Now.ToString("yyyyMMddHHmmss");
}
else
{
_strSubKey = m_strSubKey;
}
}
#endregion

#region 写入文件函数(非静态)
//写INI文件,输入键值
public void WriteValue(string strValue)
{
IniCheckDefaultValue();
WritePrivateProfileString(_strKey, _strSubKey, strValue, _strFilePath);
}
//写INI文件,输入子键名,键值
public void WriteValue(string strSubKey, string strValue)
{
IniCheckDefaultValue();
WritePrivateProfileString(_strKey, strSubKey, strValue, _strFilePath);
}
//写INI文件,输入主键名,子键名,键值
public void WriteValue(string strKey, string strSubKey, string strValue)
{
IniCheckDefaultValue();
WritePrivateProfileString(strKey, strSubKey, strValue, _strFilePath);
}
//写INI文件,输入主键名,子键名,文件路径,键值
public void WriteValue(string strKey, string strSubKey, string strPath, string strValue)
{
WritePrivateProfileString(strKey, strSubKey, strValue, strPath);
}
#endregion

#region 写入文件函数(静态)
//写INI文件,输入主键名,子键名,文件路径,键值
public static void staticWriteValue(string strKey, string strSubKey, string strPath, string strValue)
{
WritePrivateProfileString(strKey, strSubKey, strValue, strPath);
}
#endregion

#region 读取文件函数(非静态)
//读取INI文件,所有参数提前设置好
public string ReadValue()
{
StringBuilder strTemp = new StringBuilder(m_intSize);
int i = GetPrivateProfileString(m_strKey, m_strSubKey, "", strTemp, m_intSize, m_strFilePath);
return strTemp.ToString();
}
//读取INI文件,输入子键名
public string ReadValue(string strSubKey)
{
StringBuilder strTemp = new StringBuilder(m_intSize);
int i = GetPrivateProfileString(m_strKey, strSubKey, "", strTemp, m_intSize, m_strFilePath);
return strTemp.ToString();
}
//读取INI文件,输入主键名,子键名
public string ReadValue(string strKey, string strSubKey)
{
StringBuilder strTemp = new StringBuilder(m_intSize);
int i = GetPrivateProfileString(strKey, strSubKey, "", strTemp, m_intSize, m_strFilePath);
return strTemp.ToString();
}
//读取INI文件,输入主键名,子键名,文件路径
public string ReadValue(string strKey, string strSubKey, string strPath)
{
StringBuilder strTemp = new StringBuilder(m_intSize);
int i = GetPrivateProfileString(strKey, strSubKey, "", strTemp, m_intSize, strPath);
return strTemp.ToString();
}
//读取INI文件,输入主键名,子键名,取字符串长度
public string ReadValue(string strKey, string strSubKey, int intSize)
{
StringBuilder strTemp = new StringBuilder(intSize);
int i = GetPrivateProfileString(strKey, strSubKey, "", strTemp, intSize, m_strFilePath);
return strTemp.ToString();
}
//读取INI文件,输入主键名,子键名,文件路径,取字符串长度
public string ReadValue(string strKey, string strSubKey, string strPath, int intSize)
{
StringBuilder strTemp = new StringBuilder(intSize);
int i = GetPrivateProfileString(strKey, strSubKey, "", strTemp, intSize, strPath);
return strTemp.ToString();
}

#endregion

#region 读取文件函数(静态)
//读取INI文件,输入主键名,子键名,文件路径,取字符串长度
public static string staticReadValue(string strKey, string strSubKey, string strPath, int intSize)
{
StringBuilder strTemp = new StringBuilder(intSize);
int i = GetPrivateProfileString(strKey, strSubKey, "", strTemp, intSize, strPath);
return strTemp.ToString();
}

#endregion
}

public class IniFile
{

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

/// <summary>
/// 写入INI文件
/// </summary>
/// <param name="Section">节点名称</param>
/// <param name="Key">关键字</param>
/// <param name="Value">值</param>
/// <param name="filepath">INI文件路径</param>
static public void IniWriteValue(string Section, string Key, string Value, string filepath)
{
WritePrivateProfileString(Section, Key, Value, filepath);
}

/// <summary>
/// 读取INI文件
/// </summary>
/// <param name="Section">节点名称</param>
/// <param name="Key">关键字</param>
/// <param name="filepath">INI文件路径</param>
/// <returns>值</returns>
static public string IniReadValue(string Section, string Key, string filepath)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "", temp,
255, filepath);
return temp.ToString();

}
}

public class IniFile
{
[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);

/// <summary>
/// INIFile Constructor.
/// </summary>
/// <PARAM name="INIPath"></PARAM>

public string path;
public IniFile(string INIPath)
{
path = INIPath;
}

/// <summary>
/// Write Data to the INI File
/// </summary>
/// <PARAM name="Section"></PARAM>
/// Section name
/// <PARAM name="Key"></PARAM>
/// Key Name
/// <PARAM name="Value"></PARAM>
/// Value Name
public void IniWriteValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, this.path);
}

/// <summary>
/// Read Data Value From the Ini File
/// </summary>
/// <PARAM name="Section"></PARAM>
/// <PARAM name="Key"></PARAM>
/// <PARAM name="Path"></PARAM>
/// <returns></returns>
public string IniReadValue(string Section, string Key,string DefaultValue)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path);
string result =temp.ToString();
if (result.Trim() == "")
{
result = DefaultValue;
WritePrivateProfileString(Section, Key, DefaultValue, this.path);
}

return result;
}
}

实例化IniFile类(传入ini文件路径)
然后就可以IniWriteValue写和IniReadValue读了

直接读写指定段的属性更方便~
33.读取ini文件属性
//using System.Runtime.InteropServices;
//[DllImport("kernel32")]//返回取得字符串缓冲区的长度
//private static extern long GetPrivateProfileString(string section,string key, string def,StringBuilder retVal,int size,string filePath);
string Section=%%1;
string Key=%%2;
string NoText=%%3;
string iniFilePath="Setup.ini";
string %%4=String.Empty;
if(File.Exists(iniFilePath)){
StringBuilder temp = new StringBuilder(1024);
GetPrivateProfileString(Section,Key,NoText,temp,1024,iniFilePath);
%%4=temp.ToString();
}

35.写入ini文件属性
//using System.Runtime.InteropServices;
//[DllImport("kernel32")]//返回0表示失败,非0为成功
//private static extern long WritePrivateProfileString(string section,string key, string val,string filePath);
string Section=%%1;
string Key=%%2;
string Value=%%3;
string iniFilePath="Setup.ini";
bool %%4=false;
if(File.Exists(iniFilePath))
{
long OpStation = WritePrivateProfileString(Section,Key,Value,iniFilePath);
if(OpStation == 0)
{
%%4=false;
}
else
{
%%4=true;
}
}

w


潍坊市18981062189: C#读取INI文件怎么操作?
漆菡雌三: C#对INI进行读写操作如下 using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Runtime.InteropServices;namespace Update {class IniFile{private string fileName;[DllImport("kernel32")]...

潍坊市18981062189: 如何在C#应用程序中使用INI文件 -
漆菡雌三: c#的类没有直接提供对ini文件的操作支持,可以自己包装win api的WritePrivateProfileString和GetPrivateProfileString函数实现.下面提供一个包装类,可以直接使用.public class Ini { // 声明INI文件的写操作函数 WritePrivateProfileString() [...

潍坊市18981062189: 如何在C#中读写INI文件 -
漆菡雌三: public static class SIIniFile { [DllImport("kernel32")] private static extern long WritePrivateProfileString(string strSection, string strKey, string strVal, string strFilePath); [DllImport("kernel32")] private static extern int GetPrivateProfileString(string ...

潍坊市18981062189: C# ini文件操作 -
漆菡雌三: //要读写INI文件必须使用以下三个文件 using System.Runtime.InteropServices; using System.IO; using System.Text; namespace Project1 { public class WinForm : System.Windows.Forms.Form { [DllImport("kernel32")] private static extern long ...

潍坊市18981062189: c#读写INI文件 -
漆菡雌三: ini文件其实就是一个文本文件,它有固定的格式,节Section的名字用[]括起来,然后换行说明key的值:[section] key=value 如数据库服务器配置文件:DBServer.ini [Server] Name=localhost [DB] Name=NorthWind [User] Name=sa 在C#中,对配...

潍坊市18981062189: C#怎样读写ini文件,最好有例子 -
漆菡雌三: filecreate('路径加文件名');//创建一个文件. 读写ini文件: 先在 uses 定义 Inifiles, 在 var 定义 myinifile:Tinifile; 实现部分写代码: myinifile:=Tinifile.create('d:\1.ini');//打开D盘的 1.ini 文件. myinifile.readstring('小节名','关键字','缺省值'...

潍坊市18981062189: 用C#如何读写配置文件? -
漆菡雌三: INI文件就是扩展名为"ini"的文件. 其一般形式如下: [section1] // 配置节 //键名 //键值 keyword1 = valuel keyword2 = value2 …… [section2] keyword3 = value3 keyword4 = value4 在Windows系统中,INI文件是很多,最重要的就是"System.ini...

潍坊市18981062189: C#如何读取INI文件 -
漆菡雌三: ...你可以用现成的类读取 也可以读取文件流 然后做截取N多方法

潍坊市18981062189: c#读写INI文件
漆菡雌三: 调用API函数读写:1、追加引用using System.Runtime.InteropServices; 2、定义API函数////声明读写INI文件的API函数 [DllImport("kernel32")]private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);...

潍坊市18981062189: C# 读取ini文件 -
漆菡雌三: using System.Runtime.InteropServices; [DllImport("kernel32.dll")] private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); [DllImport("kernel32.dll")] private static extern int GetPrivateProfileString...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网