国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 惠水县| 墨脱县| 华蓥市| 安化县| 巧家县| 甘孜县| 台北市| 昌邑市| 长宁区| 卫辉市| 弥勒县| 遵化市| 寿光市| 公主岭市| 循化| 鄂托克前旗| 焦作市| 五家渠市| 六安市| 通城县| 德安县| 永泰县| 祁连县| 靖西县| 英德市| 南通市| 清徐县| 桃江县| 南和县| 莱州市| 依兰县| 沙坪坝区| 太仆寺旗| 定南县| 富平县| 涪陵区| 木里| 延津县| 保山市| 五莲县| 阿图什市|