天天看點

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