c#winfrom读写ini文件([set] User_ID=123 PassWord=MTIz [dev_qty] 点胶机=44 激光分板机=17 镭雕机=3)

时间: 2023-09-30 22:10:48 浏览: 68
你可以使用 `System.IO` 命名空间中的 `StreamReader` 和 `StreamWriter` 类来读写 INI 文件。下面是一个示例代码: ```csharp using System; using System.Collections.Generic; using System.IO; public class IniFile { private readonly string filePath; private Dictionary<string, Dictionary<string, string>> sections; public IniFile(string filePath) { this.filePath = filePath; sections = new Dictionary<string, Dictionary<string, string>>(); ParseIniFile(); } private void ParseIniFile() { string currentSection = null; Dictionary<string, string> currentProperties = null; foreach (string line in File.ReadLines(filePath)) { if (line.StartsWith("[") && line.EndsWith("]")) { currentSection = line.Substring(1, line.Length - 2); currentProperties = new Dictionary<string, string>(); sections[currentSection] = currentProperties; } else if (!string.IsNullOrWhiteSpace(line)) { string[] parts = line.Split('='); if (parts.Length == 2) { string key = parts[0].Trim(); string value = parts[1].Trim(); currentProperties[key] = value; } } } } public string GetValue(string section, string key) { if (sections.TryGetValue(section, out var properties)) { if (properties.TryGetValue(key, out var value)) { return value; } } return null; } public void SetValue(string section, string key, string value) { if (!sections.ContainsKey(section)) { sections[section] = new Dictionary<string, string>(); } sections[section][key] = value; } public void Save() { using (StreamWriter writer = new StreamWriter(filePath)) { foreach (var section in sections) { writer.WriteLine($"[{section.Key}]"); foreach (var property in section.Value) { writer.WriteLine($"{property.Key}={property.Value}"); } } } } } public class Program { public static void Main() { IniFile ini = new IniFile("path/to/your/file.ini"); // 读取值 string userID = ini.GetValue("set", "User_ID"); string password = ini.GetValue("set", "PassWord"); int glueMachineQty = int.Parse(ini.GetValue("dev_qty", "点胶机")); Console.WriteLine($"User ID: {userID}"); Console.WriteLine($"Password: {password}"); Console.WriteLine($"Glue Machine Qty: {glueMachineQty}"); // 设置新值 ini.SetValue("set", "User_ID", "456"); ini.SetValue("set", "PassWord", "NDU2"); ini.SetValue("dev_qty", "点胶机", "55"); // 保存更改 ini.Save(); } } ``` 将上述代码中的 `path/to/your/file.ini` 替换为你的 INI 文件的路径。这个示例代码可以读取 INI 文件中的值,并且可以设置新的值并保存更改。

相关推荐

最新推荐

recommend-type

零售管理系统_C# winfrom

零售业中小型批发、零售企业及连锁店往往存在一些共同的特点和问题,如商品品牌繁多、库存漏洞大、决策欠依据、经营不规范等,而这些问题往往是一个商家制胜的关键。如果还采用以前的手工销售、统计的方法,将会极大...
recommend-type

c#实现winform屏幕截图并保存的示例

主要介绍了c#实现winform屏幕截图并保存的示例,需要的朋友可以参考下
recommend-type

C#实现Winform中打开网页页面的方法

主要介绍了C#实现Winform中打开网页页面的方法,涉及WinForm中WebBrowser的相关使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
recommend-type

C#实现鼠标移动到曲线图上显示值的方法

主要介绍了C#实现鼠标移动到曲线图上显示值的方法,是C#的WinForm窗体程序设计中非常实用的技巧,需要的朋友可以参考下
recommend-type

C#调用webservice接口的最新方法教程

webservice 可以用于分布式...下面这篇文章主要给大家介绍了关于C#调用webservice接口的相关资料,文中通过图文介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友下面随着小编来一起看看吧。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。