C# creates ini files, reads values, and modifies values
调用方法:
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) { //如果文件不存在,则创建 if (!File.Exists(FileName)) { using (FileStream myFs = new FileStream(FileName, FileMode.Create)) { } } IniFile f = new IniFile(FileName); f.WriteContentValue(section, key, value); } }
Copy after login
核心代码:
public class IniFile { public string Path; public IniFile(string path) { this.Path = path; } /// <summary> /// 写入INI文件 /// </summary> /// <param name="section">节点名称[如[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">节点名称</param> /// <param name="key">键</param> /// <param name="def">值</param> /// <param name="retval">stringbulider对象</param> /// <param name="size">字节大小</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(); } }
Copy after login
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

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

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

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article
Assassin's Creed Shadows: Seashell Riddle Solution
1 months ago
By DDD
What's New in Windows 11 KB5054979 & How to Fix Update Issues
3 weeks ago
By DDD
Where to find the Crane Control Keycard in Atomfall
1 months ago
By DDD
How to fix KB5055523 fails to install in Windows 11?
2 weeks ago
By DDD
InZoi: How To Apply To School And University
3 weeks ago
By DDD

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)
