天天看點

C# ini檔案操作【源碼下載下傳】目錄 1. ini檔案介紹 2. GetPrivateProfileString()函數 :讀取操作 3. WritePrivateProfileString()函數:寫入操作 4. 源碼下載下傳

介紹C#如何對ini檔案進行讀寫操作,C#可以通過調用【kernel32.dll】檔案中的 WritePrivateProfileString()和GetPrivateProfileString()函數分别對ini檔案進行讀和寫操作。包括:讀取key的值、儲存key的值、讀取所有section、讀取所有key、移除section、移除key等操作。

ini檔案常用于存儲各類應用的配置資訊,而内部的檔案結構主要包括三個概念:section、key和value。

其中section為各獨立的區域塊,名稱可以為英文、中文。

C# ini檔案操作【源碼下載下傳】目錄 1. ini檔案介紹 2. GetPrivateProfileString()函數 :讀取操作 3. WritePrivateProfileString()函數:寫入操作 4. 源碼下載下傳

C#可以通過調用【kernel32.dll】檔案中的 GetPrivateProfileString()函數對ini檔案進行讀取操作。

函數簽名:

1

2

<code>[DllImport(</code><code>"kernel32"</code><code>)]</code>

<code>private</code> <code>static</code> <code>extern</code> <code>int</code> <code>GetPrivateProfileString(</code><code>string</code> <code>sectionName, </code><code>string</code> <code>key, </code><code>string</code> <code>defaultValue, </code><code>byte</code><code>[] returnBuffer, </code><code>int</code> <code>size, </code><code>string</code> <code>filePath); </code>

成員:

sectionName  {string | null}:要讀區的區域名。若傳入null值,第4個參數returnBuffer将會獲得所有的section name。

key {string | null}:key的名稱。若傳入null值,第4個參數returnBuffer将會獲得所有的指定sectionName下的所有key name。

defaultValue {string}:key沒找到時的傳回值。

returnBuffer {byte[]}:key所對應的值。

filePath {string}:ini檔案路徑。

支援的操作:

3

4

5

6

7

8

9

10

11

12

13

<code>/// &lt;summary&gt;</code>

<code>/// 根據Key讀取Value</code>

<code>/// &lt;/summary&gt;</code>

<code>/// &lt;param name="sectionName"&gt;section名稱&lt;/param&gt;</code>

<code>/// &lt;param name="key"&gt;key的名稱&lt;/param&gt;</code>

<code>/// &lt;param name="filePath"&gt;檔案路徑&lt;/param&gt;</code>

<code>public</code> <code>static</code> <code>string</code> <code>GetValue(</code><code>string</code> <code>sectionName, </code><code>string</code> <code>key, </code><code>string</code> <code>filePath)</code>

<code>{</code>

<code>    </code><code>byte</code><code>[] buffer = </code><code>new</code> <code>byte</code><code>[2048];</code>

<code>    </code><code>int</code> <code>length = GetPrivateProfileString(sectionName, key, </code><code>"發生錯誤"</code><code>, buffer,999, filePath);</code>

<code>    </code><code>string</code> <code>rs = System.Text.UTF8Encoding.Default.GetString(buffer, 0, length);</code>

<code>    </code><code>return</code> <code>rs;</code>

<code>}</code>

注意:中文名稱的section要進行轉碼。

<code>/// 擷取ini檔案内所有的section名稱</code>

<code>/// &lt;returns&gt;傳回一個包含section名稱的集合&lt;/returns&gt;</code>

<code>public</code> <code>static</code> <code>List&lt;</code><code>string</code><code>&gt; GetSectionNames(</code><code>string</code> <code>filePath)</code>

<code>    </code><code>int</code> <code>length = GetPrivateProfileString(</code><code>null</code><code>, </code><code>""</code><code>, </code><code>""</code><code>, buffer, 999, filePath);</code>

<code>    </code><code>String[] rs = System.Text.UTF8Encoding.Default.GetString(buffer, 0, length).Split(</code><code>new</code> <code>string</code><code>[] { </code><code>"\0"</code> <code>},StringSplitOptions.RemoveEmptyEntries);</code>

<code>    </code><code>return</code> <code>rs.ToList();</code>

  

同樣要對中問名稱的key進行轉碼。

<code>/// 擷取指定section内的所有key</code>

<code>/// &lt;returns&gt;傳回一個包含key名稱的集合&lt;/returns&gt;</code>

<code>public</code> <code>static</code> <code>List&lt;</code><code>string</code><code>&gt; GetKeys(</code><code>string</code> <code>sectionName, </code><code>string</code> <code>filePath)</code>

<code>    </code><code>int</code> <code>length = GetPrivateProfileString(sectionName,</code><code>null</code><code>,</code><code>""</code><code>, buffer, 999, filePath);</code>

<code>    </code><code>String[] rs = System.Text.UTF8Encoding.Default.GetString(buffer, 0, length).Split(</code><code>new</code> <code>string</code><code>[] { </code><code>"\0"</code> <code>}, StringSplitOptions.RemoveEmptyEntries);</code>

C#可以通過調用【kernel32.dll】檔案中的 WritePrivateProfileString()函數對ini檔案進行寫入操作。

<code>private</code> <code>static</code> <code>extern</code> <code>long</code> <code>WritePrivateProfileString(</code><code>string</code> <code>sectionName, </code><code>string</code> <code>key, </code><code>string</code> <code>value, </code><code>string</code> <code>filePath);</code>

sectionName {string}:要寫入的區域名。

key {string | null}:key的名稱。若傳入null值,将移除指定的section。

value {string | null}:設定key所對應的值。若傳入null值,将移除指定的key。

注意:若此key不存在将會建立,否則就為修改此key的值。

<code>/// 儲存内容到ini檔案</code>

<code>/// &lt;para&gt;若存在相同的key,就覆寫,否則就增加&lt;/para&gt;</code>

<code>/// &lt;param name="value"&gt;存儲的值&lt;/param&gt;</code>

<code>public</code> <code>static</code> <code>bool</code> <code>SetValue(</code><code>string</code> <code>sectionName, </code><code>string</code> <code>key, </code><code>string</code> <code>value, </code><code>string</code> <code>filePath)</code>

<code>    </code><code>int</code> <code>rs = (</code><code>int</code><code>)WritePrivateProfileString(sectionName, key, value, filePath);</code>

<code>    </code><code>return</code> <code>rs &gt; 0;</code>

說明:key參數傳入null就為移除指定的section。

<code>/// 移除指定的section</code>

<code>/// &lt;returns&gt;&lt;/returns&gt;</code>

<code>public</code> <code>static</code> <code>bool</code> <code>RemoveSection(</code><code>string</code> <code>sectionName, </code><code>string</code> <code>filePath)</code>

<code>    </code><code>int</code> <code>rs = (</code><code>int</code><code>)WritePrivateProfileString(sectionName, </code><code>null</code><code>, </code><code>""</code><code>, filePath);</code>

說明:value參數傳入null就為移除指定的key。

<code>/// 移除指定的key</code>

<code>public</code> <code>static</code> <code>bool</code> <code>Removekey(</code><code>string</code> <code>sectionName, </code><code>string</code> <code>key, </code><code>string</code> <code>filePath)</code>

<code>    </code><code>int</code> <code>rs = (</code><code>int</code><code>)WritePrivateProfileString(sectionName, key, </code><code>null</code><code>, filePath);</code>

C# ini檔案操作【源碼下載下傳】目錄 1. ini檔案介紹 2. GetPrivateProfileString()函數 :讀取操作 3. WritePrivateProfileString()函數:寫入操作 4. 源碼下載下傳

繼續閱讀