C#讀寫INI檔案

來源:互聯網
上載者:User

標籤:style   http   io   os   使用   ar   檔案   sp   2014   

C#讀寫INI檔案,需要用到兩個API函數

WritePrivateProfileString

GetPrivateProfileString

需要特別注意的是:這兩個函數中的*.ini檔案地址要使用絕對位址

下面程式的功能,是在一個空檔案test.ini中,寫入一些屬性,並讀取

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace IniTest{    /// <summary>    /// INI檔案工具類    /// </summary>    class IniHelper    {        /// <summary>        /// 設定INI檔案中的屬性        /// </summary>        /// <param name="section">節</param>        /// <param name="key">鍵</param>        /// <param name="val">值</param>        /// <param name="filePath">INI檔案的絕對位址</param>        /// <returns></returns>        [System.Runtime.InteropServices.DllImport("kernel32")]        private static extern long WritePrivateProfileString(            string section, string key, string val, string filePath);        /// <summary>        /// 讀取INI檔案中的屬性        /// </summary>        /// <param name="section">節</param>        /// <param name="key">鍵</param>        /// <param name="def">預設值</param>        /// <param name="retVal">被儲存到的StringBuilder</param>        /// <param name="size">最大字串截取長度</param>        /// <param name="filePath">INI檔案的絕對位址</param>        /// <returns></returns>        [System.Runtime.InteropServices.DllImport("kernel32")]        private static extern int GetPrivateProfileString(            string section, string key, string def,             System.Text.StringBuilder retVal, int size, string filePath);        /// <summary>        /// INI檔案的絕對路徑        /// </summary>        public string Path;        /// <summary>        /// 讀取的預設值        /// </summary>        public string DefValue;        /// <summary>        /// INI讀寫工具類        /// </summary>        /// <param name="path">INI檔案絕對位址</param>        /// <param name="def">讀取值的預設值</param>        public IniHelper(string path, string def = "")        {            System.IO.FileInfo fi = new System.IO.FileInfo(path);            if (!fi.Exists)            {                throw new Exception("檔案不存在");            }            this.Path = fi.FullName;            this.DefValue = def;        }        /// <summary>        /// 設定INI檔案的一個值        /// </summary>        /// <param name="section">節</param>        /// <param name="key">鍵</param>        /// <param name="value">值</param>        public void SetValue(string section, string key, object value)        {            WritePrivateProfileString(section, key, value.ToString(), this.Path);        }        /// <summary>        /// 擷取INI檔案的一個值        /// </summary>        /// <param name="section"></param>        /// <param name="key"></param>        /// <returns></returns>        public string GetValue(string section, string key)        {            StringBuilder sb = new StringBuilder(255);            int i = GetPrivateProfileString(                section, key, this.DefValue, sb, 255, this.Path);            return sb.ToString();        }    }    class Program    {        static void Main(string[] args)        {            IniHelper ih = new IniHelper("test.ini", "Default");            //設定INI檔案中的值            ih.SetValue("Set1", "attr1", "1");            ih.SetValue("Set1", "attr2", 2L);            ih.SetValue("Set1", "attr3", 3f);            ih.SetValue("Set2", "attra", new StringBuilder("a"));            ih.SetValue("Set2", "attrb", ‘b‘);            //讀取INI檔案中的值            Console.WriteLine(ih.GetValue("Set1", "attr1"));            Console.WriteLine(ih.GetValue("Set1", "attr2"));            Console.WriteLine(ih.GetValue("Set1", "attr3"));            Console.WriteLine(ih.GetValue("Set2", "attra"));            Console.WriteLine(ih.GetValue("Set2", "attrb"));            //讀取不存在的索引值            Console.WriteLine(ih.GetValue("Setx", "attrx"));            Console.WriteLine("Done.");            Console.ReadLine();        }    }}

程式運行結果

END

C#讀寫INI檔案

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.