欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#操作INI文件的示例代碼

 更新時(shí)間:2023年12月29日 15:35:10   作者:lljss2020  
這篇文章主要為大家詳細(xì)介紹了C#操作INI文件的相關(guān)知識,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以跟隨上班一起學(xué)習(xí)一下

IniFileOp.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace xxxxx
{
    class IniFileOp
    {
        //#region 聲明讀寫INI文件的API函數(shù)
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filepath);
        //參數(shù)說明:section:INI文件中的段落;key:INI文件中的關(guān)鍵字;val:INI文件中關(guān)鍵字的數(shù)值;filePath:INI文件的完整的路徑和名稱。

        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
        //參數(shù)說明:section:INI文件中的段落名稱;key:INI文件中的關(guān)鍵字;def:無法讀取時(shí)候時(shí)候的缺省數(shù)值;retVal:讀取數(shù)值;size:數(shù)值的大小;filePath:INI文件的完整路徑和名稱
        //#endregion

        public string Path;

        public IniFileOp(string path)
        {
            this.Path = path;
        }


        /// <summary>
        /// 寫INI文件
        /// </summary>
        /// <param name="section">段落</param>
        /// <param name="key">鍵</param>
        /// <param name="iValue">值</param>
        public void IniWriteStr(string section, string key, string iValue)
        {
            WritePrivateProfileString(section, key, iValue, this.Path);
        }

        /// <summary>
        /// 讀取INI文件
        /// </summary>
        /// <param name="section">段落</param>
        /// <param name="key">鍵</param>
        /// <returns>返回的鍵值</returns>
        public string IniReadStr(string section, string key)
        {
            StringBuilder sb = new StringBuilder(256);

            int i = GetPrivateProfileString(section, key, "", sb, 256, this.Path);
            return sb.ToString();
        }

        /// <summary>
        /// 讀取INI文件
        /// </summary>
        /// <param name="Section">段,格式[]</param>
        /// <param name="Key">鍵</param>
        /// <returns>返回byte類型的section組或鍵值組</returns>
        public byte[] IniReadBytes(string section, string key)
        {
            StringBuilder sb = new StringBuilder(256);

            int i = GetPrivateProfileString(section, key, "", sb, 256, this.Path);
            string s = sb.ToString();
            byte[] byteArray = System.Text.Encoding.Default.GetBytes(s);
            return byteArray;
        }
    }
}

ConfigPara.ini

[Param]
PauseTime=0

使用

 int pauseTime = 0;

 string path1 = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
 path1 += "ConfigPara.ini";
 IniFileOp inifile = new IniFileOp(path1);

 string str1 = inifile.IniReadStr("Param", "PauseTime");

 if (str1 != "")
 {
     pauseTime = int.Parse(str1);
 }

到此這篇關(guān)于C#操作INI文件的示例代碼的文章就介紹到這了,更多相關(guān)C#操作INI內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論