調用方法:
int iBaudRate = InIHelper.ReadConfigInIHelper類:
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 images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article
Guide: Stellar Blade Save File Location/Save File Lost/Not Saving
4 weeks ago
By DDD
Oguri Cap Build Guide | A Pretty Derby Musume
2 weeks ago
By Jack chen
Agnes Tachyon Build Guide | A Pretty Derby Musume
2 weeks ago
By Jack chen
Dune: Awakening - Advanced Planetologist Quest Walkthrough
4 weeks ago
By Jack chen
Date Everything: Dirk And Harper Relationship Guide
4 weeks ago
By Jack chen

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
