欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#操作INI文件的輔助類IniHelper

 更新時(shí)間:2018年03月23日 15:25:11   作者:馬洪彪  
這篇文章主要為大家詳細(xì)介紹了C#操作INI文件的輔助類IniHelper,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

使用INI配置文件,簡(jiǎn)單便捷。

該輔助工具類為C#操作INI文件的輔助類,源碼在某位師傅的基礎(chǔ)上完善的來(lái),因?yàn)橥涀畛醯膩?lái)源了,因此不能提及引用,在此深感遺憾,并對(duì)貢獻(xiàn)者表示感謝。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;

namespace Eyuan.Common
{
 public static class INIHelper
 {

  #region 讀寫INI文件相關(guān)
  [DllImport("kernel32.dll", EntryPoint = "WritePrivateProfileString", CharSet = CharSet.Ansi)]
  private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

  [DllImport("kernel32.dll", EntryPoint = "GetPrivateProfileString", CharSet = CharSet.Ansi)]
  private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

  [DllImport("kernel32")]
  private static extern int GetPrivateProfileInt(string lpApplicationName, string lpKeyName, int nDefault, string lpFileName);


  [DllImport("kernel32.dll", EntryPoint = "GetPrivateProfileSectionNames", CharSet = CharSet.Ansi)]
  private static extern int GetPrivateProfileSectionNames(IntPtr lpszReturnBuffer, int nSize, string filePath);

  [DllImport("KERNEL32.DLL ", EntryPoint = "GetPrivateProfileSection", CharSet = CharSet.Ansi)]
  private static extern int GetPrivateProfileSection(string lpAppName, byte[] lpReturnedString, int nSize, string filePath);
  #endregion

  #region 讀寫操作(字符串)
  /// <summary>
  /// 向INI寫入數(shù)據(jù)
  /// </summary>
  /// <PARAM name="Section">節(jié)點(diǎn)名</PARAM>
  /// <PARAM name="Key">鍵名</PARAM>
  /// <PARAM name="Value">值(字符串)</PARAM>
  public static void Write(string Section, string Key, string Value, string path)
  {
   WritePrivateProfileString(Section, Key, Value, path);
  }
  /// <summary>
  /// 讀取INI數(shù)據(jù)
  /// </summary>
  /// <PARAM name="Section">節(jié)點(diǎn)名</PARAM>
  /// <PARAM name="Key">鍵名</PARAM>
  /// <PARAM name="Path">值名</PARAM>
  /// <returns>值(字符串)</returns>
  public static string Read(string Section, string Key, string path)
  {
   StringBuilder temp = new StringBuilder(255);
   int i = GetPrivateProfileString(Section, Key, "", temp, 255, path);
   return temp.ToString();
  }
  #endregion

  #region 配置節(jié)信息
  /// <summary>
  /// 讀取一個(gè)ini里面所有的節(jié)
  /// </summary>
  /// <param name="sections"></param>
  /// <param name="path"></param>
  /// <returns>-1:沒(méi)有節(jié)信息,0:正常</returns>
  public static int GetAllSectionNames(out string[] sections, string path)
  {
   int MAX_BUFFER = 32767;
   IntPtr pReturnedString = Marshal.AllocCoTaskMem(MAX_BUFFER);
   int bytesReturned = GetPrivateProfileSectionNames(pReturnedString, MAX_BUFFER, path);
   if (bytesReturned == 0)
   {
    sections = null;
    return -1;
   }
   string local = Marshal.PtrToStringAnsi(pReturnedString, (int)bytesReturned).ToString();
   Marshal.FreeCoTaskMem(pReturnedString);
   //use of Substring below removes terminating null for split
   sections = local.Substring(0, local.Length - 1).Split('\0');
   return 0;
  }
  /// <summary>
  /// 返回指定配置文件下的節(jié)名稱列表
  /// </summary>
  /// <param name="path"></param>
  /// <returns></returns>
  public static List<string> GetAllSectionNames(string path)
  {
   List<string> sectionList = new List<string>();
   int MAX_BUFFER = 32767;
   IntPtr pReturnedString = Marshal.AllocCoTaskMem(MAX_BUFFER);
   int bytesReturned = GetPrivateProfileSectionNames(pReturnedString, MAX_BUFFER, path);
   if (bytesReturned != 0)
   {
    string local = Marshal.PtrToStringAnsi(pReturnedString, (int)bytesReturned).ToString();
    Marshal.FreeCoTaskMem(pReturnedString);
    sectionList.AddRange(local.Substring(0, local.Length - 1).Split('\0'));
   }
   return sectionList;
  }

  /// <summary>
  /// 得到某個(gè)節(jié)點(diǎn)下面所有的key和value組合
  /// </summary>
  /// <param name="section">指定的節(jié)名稱</param>
  /// <param name="keys">Key數(shù)組</param>
  /// <param name="values">Value數(shù)組</param>
  /// <param name="path">INI文件路徑</param>
  /// <returns></returns>
  public static int GetAllKeyValues(string section, out string[] keys, out string[] values, string path)
  {
   byte[] b = new byte[65535];//配置節(jié)下的所有信息
   GetPrivateProfileSection(section, b, b.Length, path);
   string s = System.Text.Encoding.Default.GetString(b);//配置信息
   string[] tmp = s.Split((char)0);//Key\Value信息
   List<string> result = new List<string>();
   foreach (string r in tmp)
   {
    if (r != string.Empty)
     result.Add(r);
   }
   keys = new string[result.Count];
   values = new string[result.Count];
   for (int i = 0; i < result.Count; i++)
   {
    string[] item = result[i].Split(new char[] { '=' });//Key=Value格式的配置信息
    //Value字符串中含有=的處理,
    //一、Value加"",先對(duì)""處理
    //二、Key后續(xù)的都為Value
    if (item.Length > 2)
    {
     keys[i] = item[0].Trim();
     values[i] = result[i].Substring(keys[i].Length + 1);
    }
    if (item.Length == 2)//Key=Value
    {
     keys[i] = item[0].Trim();
     values[i] = item[1].Trim();
    }
    else if (item.Length == 1)//Key=
    {
     keys[i] = item[0].Trim();
     values[i] = "";
    }
    else if (item.Length == 0)
    {
     keys[i] = "";
     values[i] = "";
    }
   }
   return 0;
  }
  /// <summary>
  /// 得到某個(gè)節(jié)點(diǎn)下面所有的key
  /// </summary>
  /// <param name="section">指定的節(jié)名稱</param>
  /// <param name="keys">Key數(shù)組</param>
  /// <param name="path">INI文件路徑</param>
  /// <returns></returns>
  public static int GetAllKeys(string section, out string[] keys, string path)
  {
   byte[] b = new byte[65535];

   GetPrivateProfileSection(section, b, b.Length, path);
   string s = System.Text.Encoding.Default.GetString(b);
   string[] tmp = s.Split((char)0);
   ArrayList result = new ArrayList();
   foreach (string r in tmp)
   {
    if (r != string.Empty)
     result.Add(r);
   }
   keys = new string[result.Count];
   for (int i = 0; i < result.Count; i++)
   {
    string[] item = result[i].ToString().Split(new char[] { '=' });
    if (item.Length == 2)
    {
     keys[i] = item[0].Trim();
    }
    else if (item.Length == 1)
    {
     keys[i] = item[0].Trim();
    }
    else if (item.Length == 0)
    {
     keys[i] = "";
    }
   }
   return 0;
  }
  /// <summary>
  /// 獲取指定節(jié)下的Key列表
  /// </summary>
  /// <param name="section">指定的節(jié)名稱</param>
  /// <param name="path">配置文件名稱</param>
  /// <returns>Key列表</returns>
  public static List<string> GetAllKeys(string section, string path)
  {
   List<string> keyList = new List<string>();
   byte[] b = new byte[65535];
   GetPrivateProfileSection(section, b, b.Length, path);
   string s = System.Text.Encoding.Default.GetString(b);
   string[] tmp = s.Split((char)0);
   List<string> result = new List<string>();
   foreach (string r in tmp)
   {
    if (r != string.Empty)
     result.Add(r);
   }
   for (int i = 0; i < result.Count; i++)
   {
    string[] item = result[i].Split(new char[] { '=' });
    if (item.Length == 2 || item.Length == 1)
    {
     keyList.Add(item[0].Trim());
    }
    else if (item.Length == 0)
    {
     keyList.Add(string.Empty);
    }
   }
   return keyList;
  }
  /// <summary>
  /// 獲取值
  /// </summary>
  /// <param name="section"></param>
  /// <param name="path"></param>
  /// <returns></returns>
  public static List<string> GetAllValues(string section, string path)
  {
   List<string> keyList = new List<string>();
   byte[] b = new byte[65535];
   GetPrivateProfileSection(section, b, b.Length, path);
   string s = System.Text.Encoding.Default.GetString(b);
   string[] tmp = s.Split((char)0);
   List<string> result = new List<string>();
   foreach (string r in tmp)
   {
    if (r != string.Empty)
     result.Add(r);
   }
   for (int i = 0; i < result.Count; i++)
   {
    string[] item = result[i].Split(new char[] { '=' });
    if (item.Length == 2 || item.Length == 1)
    {
     keyList.Add(item[1].Trim());
    }
    else if (item.Length == 0)
    {
     keyList.Add(string.Empty);
    }
   }
   return keyList;
  }
  
  #endregion

  #region 通過(guò)值查找鍵(一個(gè)節(jié)中的鍵唯一,可能存在多個(gè)鍵值相同,因此反查的結(jié)果可能為多個(gè))

  /// <summary>
  /// 第一個(gè)鍵
  /// </summary>
  /// <param name="section"></param>
  /// <param name="path"></param>
  /// <param name="value"></param>
  /// <returns></returns>
  public static string GetFirstKeyByValue(string section, string path, string value)
  {
   foreach (string key in GetAllKeys(section, path))
   {
    if (ReadString(section, key, "", path) == value)
    {
     return key;
    }
   }
   return string.Empty;
  }
  /// <summary>
  /// 所有鍵
  /// </summary>
  /// <param name="section"></param>
  /// <param name="path"></param>
  /// <param name="value"></param>
  /// <returns></returns>
  public static List<string> GetKeysByValue(string section, string path, string value)
  {
   List<string > keys = new List<string>();
   foreach (string key in GetAllKeys(section, path))
   {
    if (ReadString(section, key, "", path) == value)
    {
     keys.Add(key);
    }
   }
   return keys;
  }
  #endregion


  #region 具體類型的讀寫

  #region string
  /// <summary>
  /// 
  /// </summary>
  /// <param name="sectionName"></param>
  /// <param name="keyName"></param>
  /// <param name="defaultValue" />
  /// <param name="path"></param>
  /// <returns></returns>
  public static string ReadString(string sectionName, string keyName, string defaultValue, string path)
  {
   const int MAXSIZE = 255;
   StringBuilder temp = new StringBuilder(MAXSIZE);
   GetPrivateProfileString(sectionName, keyName, defaultValue, temp, 255, path);
   return temp.ToString();
  }

  /// <summary>
  /// 
  /// </summary>
  /// <param name="sectionName"></param>
  /// <param name="keyName"></param>
  /// <param name="value"></param>
  /// <param name="path"></param>
  public static void WriteString(string sectionName, string keyName, string value, string path)
  {
   WritePrivateProfileString(sectionName, keyName, value, path);
  }
  #endregion

  #region Int
  /// <summary>
  /// 
  /// </summary>
  /// <param name="sectionName"></param>
  /// <param name="keyName"></param>
  /// <param name="defaultValue"></param>
  /// <param name="path"></param>
  /// <returns></returns>
  public static int ReadInteger(string sectionName, string keyName, int defaultValue, string path)
  {

   return GetPrivateProfileInt(sectionName, keyName, defaultValue, path);

  }
  /// <summary>
  /// 
  /// </summary>
  /// <param name="sectionName"></param>
  /// <param name="keyName"></param>
  /// <param name="value"></param>
  /// <param name="path"></param>
  public static void WriteInteger(string sectionName, string keyName, int value, string path)
  {

   WritePrivateProfileString(sectionName, keyName, value.ToString(), path);

  }
  #endregion

  #region bool
  /// <summary>
  /// 讀取布爾值
  /// </summary>
  /// <param name="sectionName"></param>
  /// <param name="keyName"></param>
  /// <param name="defaultValue"></param>
  /// <param name="path"></param>
  /// <returns></returns>
  public static bool ReadBoolean(string sectionName, string keyName, bool defaultValue, string path)
  {

   int temp = defaultValue ? 1 : 0;

   int result = GetPrivateProfileInt(sectionName, keyName, temp, path);

   return (result == 0 ? false : true);

  }
  /// <summary>
  /// 寫入布爾值
  /// </summary>
  /// <param name="sectionName"></param>
  /// <param name="keyName"></param>
  /// <param name="value"></param>
  /// <param name="path"></param>
  public static void WriteBoolean(string sectionName, string keyName, bool value, string path)
  {
   string temp = value ? "1 " : "0 ";
   WritePrivateProfileString(sectionName, keyName, temp, path);
  }
  #endregion

  #endregion

  #region 刪除操作
  /// <summary>
  /// 刪除指定項(xiàng)
  /// </summary>
  /// <param name="sectionName"></param>
  /// <param name="keyName"></param>
  /// <param name="path"></param>
  public static void DeleteKey(string sectionName, string keyName, string path)
  {
   WritePrivateProfileString(sectionName, keyName, null, path);
  }

  /// <summary>
  /// 刪除指定節(jié)下的所有項(xiàng)
  /// </summary>
  /// <param name="sectionName"></param>
  /// <param name="path"></param>
  public static void EraseSection(string sectionName, string path)
  {
   WritePrivateProfileString(sectionName, null, null, path);
  }
  #endregion

  #region 判斷節(jié)、鍵是否存在
  /// <summary>
  /// 指定節(jié)知否存在
  /// </summary>
  /// <param name="section"></param>
  /// <param name="fileName"></param>
  /// <returns></returns>
  public static bool ExistSection(string section, string fileName)
  {
   string[] sections = null;
   GetAllSectionNames(out sections, fileName);
   if (sections != null)
   {
    foreach (var s in sections)
    {
     if (s == section)
     {
      return true;
     }
    }
   }
   return false;
  }
  /// <summary>
  /// 指定節(jié)下的鍵是否存在
  /// </summary>
  /// <param name="section"></param>
  /// <param name="key"></param>
  /// <param name="fileName"></param>
  /// <returns></returns>
  public static bool ExistKey(string section, string key, string fileName)
  {
   string[] keys = null;
   GetAllKeys(section, out keys, fileName);
   if (keys != null)
   {
    foreach (var s in keys)
    {
     if (s == key)
     {
      return true;
     }
    }
   }
   return false;
  }
  #endregion

  #region 同一Section下添加多個(gè)Key\Value
  /// <summary>
  /// 
  /// </summary>
  /// <param name="section"></param>
  /// <param name="keyList"></param>
  /// <param name="valueList"></param>
  /// <param name="path"></param>
  /// <returns></returns>
  public static bool AddSectionWithKeyValues(string section, List<string> keyList, List<string> valueList, string path)
  {
   bool bRst = true;
   //判斷Section是否已經(jīng)存在,如果存在,返回false
   //已經(jīng)存在,則更新
   //if (GetAllSectionNames(path).Contains(section))
   //{
   // return false;
   //}
   //判斷keyList中是否有相同的Key,如果有,返回false

   //添加配置信息
   for (int i = 0; i < keyList.Count; i++)
   {
    WriteString(section, keyList[i], valueList[i], path);
   }
   return bRst;
  }
  #endregion
 }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

  • WPF MVVM制作發(fā)送短信小按鈕

    WPF MVVM制作發(fā)送短信小按鈕

    這篇文章主要為大家詳細(xì)介紹了WPF MVVM發(fā)送短信小按鈕的制作方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • C#基于面向過(guò)程計(jì)算加權(quán)平均分的方法

    C#基于面向過(guò)程計(jì)算加權(quán)平均分的方法

    這篇文章主要介紹了C#基于面向過(guò)程計(jì)算加權(quán)平均分的方法,涉及C#數(shù)學(xué)運(yùn)算的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-07-07
  • C#服務(wù)器NFS共享文件夾搭建與上傳圖片文件的實(shí)現(xiàn)

    C#服務(wù)器NFS共享文件夾搭建與上傳圖片文件的實(shí)現(xiàn)

    本文主要介紹了C#服務(wù)器NFS共享文件夾搭建與上傳圖片文件的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • C#中?、?.、??、??=運(yùn)算符的用法

    C#中?、?.、??、??=運(yùn)算符的用法

    本文主要介紹了C#中?、?.、??、??=運(yùn)算符的用法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • c# 實(shí)現(xiàn)打印機(jī)狀態(tài)查詢與阻塞打印

    c# 實(shí)現(xiàn)打印機(jī)狀態(tài)查詢與阻塞打印

    這篇文章主要介紹了c# 引用System.Management來(lái)實(shí)現(xiàn)對(duì)打印機(jī)狀態(tài)的查詢,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-05-05
  • 解析數(shù)字簽名的substring結(jié)構(gòu)(獲取數(shù)字簽名時(shí)間)

    解析數(shù)字簽名的substring結(jié)構(gòu)(獲取數(shù)字簽名時(shí)間)

    解析數(shù)字簽名的substring結(jié)構(gòu),大家參考使用吧
    2013-12-12
  • asp.net新聞列表生成靜態(tài)頁(yè)之批量和單頁(yè)生成

    asp.net新聞列表生成靜態(tài)頁(yè)之批量和單頁(yè)生成

    web程序的高訪問(wèn)量、大數(shù)據(jù)量、高效的用戶體驗(yàn)度,使靜態(tài)頁(yè)技術(shù)在越來(lái)越多的網(wǎng)站上發(fā)揮作用。這篇文章主要介紹asp.net新聞列表生成靜態(tài)頁(yè)之批量和單頁(yè)生成,有需要的朋友可以參考下
    2015-08-08
  • C#中的GDI+圖像編程詳解

    C#中的GDI+圖像編程詳解

    本文詳細(xì)講解了C#中的GDI+圖像編程,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05
  • 最新評(píng)論