向空文件寫數據,並讀取配置文件數據

C#讀寫INI文件,需要用到兩個API函數

WritePrivateProfileString

GetPrivateProfileString

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

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



using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConsoleApplication2.Function;


namespace ConsoleApplication2
{
    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();
        }


        static void Main(string[] args)
        {
            IniHelper ih = new IniHelper("Send91uMsg.ini", "Default");


            //設置INI文件中的值
            ih.SetValue("SEND91UMSG", "FROM","91000");
            ih.SetValue("SEND91UMSG", "GROUPS", "175088");
            ih.SetValue("SEND91UMSG", "msg", "讀取INI文件的數據");


            string fromID = ih.GetValue("SEND91UMSG","FROM");
            string toGroupID = ih.GetValue("SEND91UMSG","GROUPS");
            string msg = ih.GetValue("SEND91UMSG", "msg");
            SendMsgGroups send2group = new SendMsgGroups();
            send2group.SendMsg2Groups(fromID, toGroupID,msg);
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章