国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home 類庫下載 C# class library C# creates ini files, reads values, and modifies values

C# creates ini files, reads values, and modifies values

Nov 10, 2016 am 09:21 AM

調用方法:

int iBaudRate = InIHelper.ReadConfig("COM_SETUP", "波特率")

InIHelper類:

public class InIHelper
    {
        private static string FileName = Application.StartupPath + "\\AppConfig.ini";
        /// <summary>
        /// 讀取配置文件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static T ReadConfig<T>(string section, string key)
        {
            if (File.Exists(FileName))
            {
                IniFile f = new IniFile(FileName);
                string value = f.ReadContentValue(section, key);
 
                if (String.IsNullOrWhiteSpace(value))
                    return default(T);
 
                if (typeof(T).IsEnum)
                    return (T)Enum.Parse(typeof(T), value, true);
 
                return (T)Convert.ChangeType(value, typeof(T));
            }
            else
            {
                return default(T);
            }
        }
 
        /// <summary>
        /// 寫配置文件
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void WriteConfig(string section, string key, string value)
        {
            //如果文件不存在,則創(chuàng)建
            if (!File.Exists(FileName))
            {
                using (FileStream myFs = new FileStream(FileName, FileMode.Create)) { }
            }
 
            IniFile f = new IniFile(FileName);
            f.WriteContentValue(section, key, value);
        }
    }

核心代碼:

public class IniFile
    {
        public string Path;
 
        public IniFile(string path)
        {
            this.Path = path;
        }
 
        /// <summary>
        /// 寫入INI文件
        /// </summary>
        /// <param name="section">節(jié)點名稱[如[TypeName]]</param>
        /// <param name="key">鍵</param>
        /// <param name="val">值</param>
        /// <param name="filepath">文件路徑</param>
        /// <returns></returns>
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filepath);
 
        /// <summary>
        /// 讀取INI文件
        /// </summary>
        /// <param name="section">節(jié)點名稱</param>
        /// <param name="key">鍵</param>
        /// <param name="def">值</param>
        /// <param name="retval">stringbulider對象</param>
        /// <param name="size">字節(jié)大小</param>
        /// <param name="filePath">文件路徑</param>
        /// <returns></returns>
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retval, int size, string filePath);
 
        /// <summary>
        /// 寫入
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <param name="iValue"></param>
        public void WriteContentValue(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 ReadContentValue(string Section, string key)
        {
            StringBuilder temp = new StringBuilder(1024);
            GetPrivateProfileString(Section, key, "", temp, 1024, this.Path);
            return temp.ToString();
        }
    }


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)