国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

C#操作INI文件

2019-11-14 16:35:20
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

最近用到保存文件的相關(guān)東西,主要是封裝兩個(gè)函數(shù),代碼如下:可以直接使用

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關(guān)聯(lián)的文件        /// </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>        /// 寫(xiě)入section 鍵值對(duì),如果沒(méi)有section 則創(chuàng)建并寫(xiě)入        /// </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的值,沒(méi)有則返回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;        }     }}

  


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 锦州市| 福安市| 西乌珠穆沁旗| 怀来县| 隆林| 高邑县| 万宁市| 贺州市| 普陀区| 龙门县| 栾川县| 绩溪县| 张北县| 龙南县| 彰化市| 商丘市| 互助| 鹤壁市| 咸丰县| 边坝县| 敖汉旗| 辽中县| 金山区| 含山县| 济宁市| 常德市| 长葛市| 新昌县| 乌兰浩特市| 博白县| 都昌县| 昌江| 闽清县| 晋州市| 鄢陵县| 黄梅县| 旬阳县| 洛浦县| 阿坝| 西盟| 龙里县|