C#操作Ini檔案類
在Windows系統中,INI檔案是很多,最重要的就是“System.ini”、“System32.ini”和“Win.ini”。該檔案主要存放使用者所做的選擇以及系統的各種引數。使用者可以通過修改INI檔案,來改變應用程式和系統的很多配置。但自從Windows 95的退出,在Windows系統中引入了登錄檔的概念,INI檔案在Windows系統的地位就開始不斷下滑,這是因為登錄檔的獨特優點,使應用程式和系統都把許多引數和初始化資訊放進了登錄檔中。但在某些場合,INI檔案還擁有其不可替代的地位。由於C#類庫中並不包含讀取ini檔案,用C#讀取ini檔案,必要用到windows的API函式,所以在宣告windows的API函式必須這樣宣告:
//宣告讀寫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檔案結構
INI檔案是一種按照特點方式排列的文字檔案。每一個INI檔案結構都非常類似,由若干段落(section)組成,在每個帶括號的標題下面,是若干個以單個單詞開頭的關鍵詞(keyword)和一個等號,等號右邊的就是關鍵字對應的值(value)。其一般形式如下:
[Section1]- KeyWord1 = Valuel
- KeyWord2 = Value2
- ……
- [Section2]
- KeyWord3 = Value3
- KeyWord4 = Value4
- using System;
-
using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Collections.Specialized;
- using System.IO;
- using System.Runtime.InteropServices;
- using System.Windows.Forms;
- namespace Comm
- {
- /// <summary>
- /// IniFiles的類
- /// </summary>
- publicclass IniFiles
- {
- publicstring FileName; //INI檔名
- //string path = System.IO.Path.Combine(Application.StartupPath,"pos.ini");
- //宣告讀寫INI檔案的API函式
- [DllImport("kernel32")]
- privatestaticexternbool WritePrivateProfileString(string section, string key, string val, string filePath);
- [DllImport("kernel32")]
- privatestaticexternint GetPrivateProfileString(string section, string key, string def, byte[] retVal, int size, string filePath);
- //類的建構函式,傳遞INI檔名
- public IniFiles(string AFileName)
- {
- // 判斷檔案是否存在
- FileInfo fileInfo = new FileInfo(AFileName);
- //Todo:搞清列舉的用法
- if ((!fileInfo.Exists))
- { //|| (FileAttributes.Directory in fileInfo.Attributes))
- //檔案不存在,建立檔案
- StreamWriter sw = new StreamWriter(AFileName, false, System.Text.Encoding.Default);
- //try
- //{
- // sw.Write("#表格配置檔案");
- // sw.Close();
- // }
- // catch
- // {
- // throw (new ApplicationException("Ini檔案不存在"));
- // }
- }
- //必須是完全路徑,不能是相對路徑
- FileName = fileInfo.FullName;
- }
- //寫INI檔案
- publicvoid WriteString(string Section, string Ident, string Value)
- {
- if (!WritePrivateProfileString(Section, Ident, Value, FileName))
- {
- throw (new ApplicationException("寫Ini檔案出錯"));
- }
- }
- //讀取INI檔案指定
- publicstring ReadString(string Section, string Ident, string Default)
- {
- Byte[] Buffer = new Byte[65535];
- int bufLen = GetPrivateProfileString(Section, Ident, Default, Buffer, Buffer.GetUpperBound(0), FileName);
- //必須設定0(系統預設的內碼表)的編碼方式,否則無法支援中文
- string s = Encoding.GetEncoding(0).GetString(Buffer);
- s = s.Substring(0, bufLen);
- return s.Trim();
- }
- //讀整數
- publicint 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;
- }
- }
- //寫整數
- publicvoid WriteInteger(string Section, string Ident, int Value)
- {
- WriteString(Section, Ident, Value.ToString());
- }
- //讀布林
- publicbool 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
- publicvoid WriteBool(string Section, string Ident, bool Value)
- {
- WriteString(Section, Ident, Convert.ToString(Value));
- }
- //從Ini檔案中,將指定的Section名稱中的所有Ident新增到列表中
- publicvoid ReadSection(string Section, StringCollection Idents)
- {
- Byte[] Buffer = new Byte[16384];
- //Idents.Clear();
- int bufLen = GetPrivateProfileString(Section, null, null, Buffer, Buffer.GetUpperBound(0), FileName);
- //對Section進行解析
- GetStringsFromBuffer(Buffer, bufLen, Idents);
- }
- privatevoid 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的名稱
- publicvoid ReadSections(StringCollection SectionList)
- {
- //Note:必須得用Bytes來實現,StringBuilder只能取到第一個Section
- byte[] Buffer = newbyte[65535];
- int bufLen = 0;
- bufLen = GetPrivateProfileString(null, null, null, Buffer,
- Buffer.GetUpperBound(0), FileName);
- GetStringsFromBuffer(Buffer, bufLen, SectionList);
- }
-
相關推薦
C# 操作INI檔案類
//補充: //using System; //using System.Runtime.InteropServices; //using System.Text; //using System.Collections;
C#操作Ini檔案類
在Windows系統中,INI檔案是很多,最重要的就是“System.ini”、“System32.ini”和“Win.ini”。該檔案主要存放使用者所做的選擇以及系統的各種引數。使用者可以通過修改INI檔案,來改變應用程式和系統的很多配置。但自從Windows
C#操作ini檔案
詳細資訊直接在程式碼裡寫 首先看一下ini檔案的格式 一個鍵對應一個值 而"[網址資訊]"類似於專案名稱,將裡邊的內容分為一塊一塊就是方法中傳入的引數“Section” 對於ini檔案的註釋,必須在空的一行中進行註釋,否則像csdn=www.csdn.net;此為CSDN官方網址 程式碼在讀
C# 操作INI檔案的函式 INIClass
C# CODE: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace XXX { public class INIClass
c#之操作ini檔案
原文地址:原文地址 public class Win32API { #region INI檔案操作 /* * 針對INI檔案的API操作方法,其中的節點(Section)、鍵(KEY)都不區分大小寫 *
C# 操作INI配置檔案
重構YH是遇到這個一樣問題,就是YH前臺頁面的按鈕要求實現客戶自定義,客戶可以跟隨自己的喜好安排每個按鈕的功能,所以,在每一次客戶登陸是就需要初始化客戶自定義排版,這裡就需要用到初始化檔案——INI。 .ini 檔案是Initialization File的縮寫,即初始化
操作INI檔案函式
操作INI檔案的函式主要有: 函式名 功能 備註 GetPrivateProfileInt &nbs
c#操作壓縮檔案(ICSharpCode)
網上轉過來的,經測試很好用。 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; usin
c# 操作xml檔案,新增、刪除節點
/// <summary> /// 刪除當前選擇節點 /// </summary> /// <param name="sender"></param>
C/C++ 解析ini檔案
C/C++ 解析ini檔案 c++版本 /* * inicpp.h * * Created on: 26 Dec 2015 * Author: Fabian Meyer * License: MIT */ #ifndef INICPP_H_ #define
BCB對如何操作ini檔案?
.ini 檔案是Initialization File的縮寫,即初始化檔案,是windows的系統配置檔案所採用的儲存格式,統管windows的各項配置,在實際開發中, ini檔案的應
C# 建立INI檔案,寫入並可讀取
using System.Text; using System.IO; using System.Runtime.InteropServices; namespace HotelSystemORM.Unitl { public class IniFiles {
python 操作ini檔案
例子: [ZIP] EngineVersion=0 DATVersion=5127 FileName=dat-5127.zip FilePath=/pub/antivirus/datfiles/4.x/ FileS
.NET平臺開源專案速覽(16)C#寫PDF檔案類庫PDF File Writer介紹
1年前,我在文章:這些.NET開源專案你知道嗎?.NET平臺開源文件與報表處理元件集合(三)中(第9個專案),給大家推薦了一個開源免費的PDF讀寫元件 PDFSharp,PDFSharp我2年前就看過,用過簡單的例子,不過程式碼沒有寫成專門的文章。最近在查詢資料的時候,又發現一款小巧的寫PDF檔案
Linux下讀取Ini檔案類
#include "Ini.h" /****************************************************************************** * 功 能:建構函式 * 參 數:無 * 返回值:無 * 備 注: *********************
VC操作INI檔案 ,INI檔案操作總結,如何操作INI檔案,INI檔案使用方法小結
INI檔案簡介 在我們寫程式時,總有一些配置資訊需要儲存下來,以便在下一次啟動程式完成初始化,這實際上是一種類持久化。將一些資訊寫入INI檔案(initialization file)中,可完成簡單的持久化支援。 Windows提供了API介面用於操作INI檔案,其支援
C#操作INI文件之Vini.cs的使用
write 相對路徑 ref true rop except enc ram pat 目錄 概要 Constructor 構造器 VINI(string)
c#操作pdf的類庫
1. Aspose. Pdf. Kit是個.NET 元件,用於編輯存在的PDF檔案。你可以利用它新增或移除數字簽名,加密或解密PDF檔案。它支援大量有用的功能,比如PDF圖章建立、新增CJK字型, 新增水印或Logo,解壓或新增圖片和文字,新增書籤,轉換PDF檔案為TIFF
C#操作txt檔案以及注意事項
特別注意:讀取亂碼的話,用Encoding.Default即可。(獲取系統的當前ANSI內碼表的編碼)讀取txt檔案 如果你要讀取的檔案內容不是很多,可以使用 File.ReadAllText(filePath) 或指定編碼方式 File.ReadAllText(File
C#操作MySQL的類
and sfc comm delete class mar htable service clas C#操作MySQL的類 public class MySqlService { private static log4net.ILog