天天看点

C# Ini读写类

using System;

using System.Collections.Generic;

using System.Runtime.InteropServices;

using System.IO;

using System.Text;

namespace BRO

{

public class INIClass

{

public string 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);

[DllImport("kernel32")]

private static extern long WritePrivateProfileSection(string section, string val, string filePath);

[DllImport("kernel32")]

private static extern int GetPrivateProfileString(string section, string key, string def, Byte[] retVal, int size, string filePath);

/// <summary>

/// 构造方法

/// </summary>

/// <param name="INIPath">文件路径</param>

public INIClass(string INIPath)

{

inipath = INIPath;

if (inipath == null || inipath.Length < 1 || !ExistINIFile)

{

File.Create(INIPath).Close ();

}

}

/// <summary>

/// 写入INI文件

/// </summary>

/// <param name="Section">项目名称(如 [TypeName] )</param>

/// <param name="Key">键</param>

/// <param name="Value">值</param>

public void IniWriteValue(string Section, string Key, object objValue)

{

if (objValue == null) objValue = "";

WritePrivateProfileString(Section, Key, objValue.ToString ().Trim (), this.inipath);

}

/// <summary>

/// 写入INI文件,没有健只有值

/// </summary>

/// <param name="Section">项目名称(如 [TypeName] )</param>

/// <param name="Value">值</param>

public void IniWriteSection(string section, string val)

{

WritePrivateProfileSection(section, val, this.inipath);

}

/// <summary>

/// 读出INI文件

/// </summary>

/// <param name="Section">项目名称(如 [TypeName] )</param>

/// <param name="Key">键</param>

public string IniReadValue(string Section, string Key)

{

StringBuilder temp = new StringBuilder(500);

int i = GetPrivateProfileString(Section, Key, "", temp, 500, this.inipath);

return temp.ToString();

}

/// <summary>

/// 读出INI文件数组 王军

/// </summary>

/// <param name="Section">项目名称(如 [TypeName] )</param>

/// <param name="Key">键</param>

public byte[] IniReadValueNew(string Section, string Key)

{

byte[] temp = new byte[500];

int i = GetPrivateProfileString(Section, Key, "", temp, 500, this.inipath);

return temp;

}

/// <summary>

/// 验证文件是否存在

/// </summary>

/// <returns>布尔值</returns>

public bool ExistINIFile

{

get{ return File.Exists(inipath); }

}

}

}

在读写的时候可以使用静态方法 例:

static INIClass m_IniFileUserOption = null;

public static string ReadOptionUser(string sSectiong, string sKey)

{

if (m_IniFileUserOption == null) return "";

string strValue = m_IniFileUserOption.IniReadValue(sSectiong, sKey);

return strValue;

}

public static void WriteOptionUser(string sSectiong, string sKey, object objValue)

{

if (m_IniFileUserOption == null) return;

// string strValue = Cryptogram.EncryptDES(objValue.ToString(), "Ki8kQi9p");

m_IniFileUserOption.IniWriteValue(sSectiong, sKey, objValue);