C# 操作INI檔案

來源:互聯網
上載者:User

標籤:blog   使用   檔案   art   cti   for   

最近用到儲存檔案的相關東西,主要是封裝兩個函數,代碼如下:可以直接使用

using System;using System.Collections.Specialized;using System.IO;using System.Runtime.InteropServices;using System.Text;namespace IniFileTest{    public class INI    {        const int COUNT = 0xFFFF;        [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[] buffer, int size, string filePath);        /// <summary>        /// ini關聯的檔案        /// </summary>        private string FileName;        public INI(string filename)        {            FileInfo file = new FileInfo(filename);            if (file.Exists)            {                FileName = new FileInfo(filename).FullName;            }        }        /// <summary>        /// 刪除Section        /// </summary>        /// <param name="SectionName"></param>        /// <returns></returns>        public bool EraseSection(string SectionName)        {            return WritePrivateProfileString(SectionName, null, null, FileName);        }        /// <summary>        /// 寫入section 索引值對,如果沒有section 則建立並寫入        /// </summary>        /// <param name="section"></param>        /// <param name="key"></param>        /// <param name="value"></param>        /// <returns></returns>        public bool Write(string section, string key, string value)        {            return WritePrivateProfileString(section, key, value, FileName);        }        /// <summary>        /// 刪除鍵        /// </summary>        /// <param name="section"></param>        /// <param name="key"></param>        /// <returns></returns>        public bool EraseKey(string section, string key)        {            return WritePrivateProfileString(section, key, null, FileName);        }        /// <summary>        /// 擷取所有section        /// </summary>        /// <returns></returns>        public StringCollection GetSections()        {            StringCollection sections = new StringCollection();            byte[] buffer = new byte[COUNT];            int length = GetPrivateProfileString(null, null, null, buffer, COUNT, FileName);            int start = 0;            for (int i = 0; i < length; i++)            {                if (buffer[i] == 0 && i > start)                {                    string str = Encoding.Default.GetString(buffer, start, i - start);                    sections.Add(str);                    start = i + 1; ;                }            }            return sections;        }        /// <summary>        /// 擷取section下的所喲keys        /// </summary>        /// <param name="section"></param>        /// <returns></returns>        public StringCollection GetSectionKeys(string section)        {            StringCollection keys = new StringCollection();            byte[] buffer = new byte[COUNT];            int length = GetPrivateProfileString(section, null, null, buffer, COUNT, FileName);            int start = 0;            for (int i = 0; i < length; i++)            {                if (buffer[i] == 0 && i > start)                {                    string str = Encoding.Default.GetString(buffer, start, i - start);                    keys.Add(str);                    start = i + 1; ;                }            }            return keys;        }        /// <summary>        /// 查尋指定的key的值,沒有則返回defaultvalue        /// </summary>        /// <param name="section"></param>        /// <param name="key"></param>        /// <param name="defaultvalue"></param>        /// <returns></returns>        public String GetValue(String section, string key, string defaultvalue)        {            byte[] bytes = new byte[COUNT];            int s = GetPrivateProfileString(section, key, defaultvalue, bytes, COUNT, FileName);            string str = Encoding.GetEncoding(0).GetString(bytes, 0, s);            return defaultvalue;        }     }}

  

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.