当前位置:网站首页>C # read INI file and key value pair operation
C # read INI file and key value pair operation
2022-07-28 14:31:00 【CDamogu】
.Net practice -C# How to read 、 write in INI The configuration file

Catalog
C# Read ini file
sketch
I've been in contact with INI Read and write configuration files , Although a long time ago, Microsoft recommended using the registry instead INI The configuration file , Now in Visual Studio There are also special .Net Profile format , But it still looks like INI Profile pleasing to the eye . in fact .Net Of XML The format configuration file is more powerful , I also recommend You use this type of configuration file to .Net Software development , The reason why I use INI The configuration file , Just want to taste fresh and personal habits .
C# It does not provide access INI How to configure files , But we can use WinAPI Provide methods to deal with INI Reading and writing of documents , The code is simple ! There are a lot of ready-made code on the Internet , Here is just for recording and sorting , Convenient for future use .
INI Configuration file composition
INI Files are text files , It consists of several sections (section) form , Under each bracketed section name , There are several key words (key) And its corresponding value (Value), These keywords (key) Belong to the keyword (key) Section on (section).
[Section]
Key1=Value1
Key2=Value2
Core code
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace ToolsLibrary
{
public class IniFile
{
public string path; //INI file name
// The statement says INI Of documents API function
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
// Statement read INI Of documents API function
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
// Class constructor , Pass on INI The path and filename of the file
public IniFile(string INIPath)
{
path = INIPath;
}
// Write INI file
public void IniWriteValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, path);
}
// Read INI file
public string IniReadValue(string Section, string Key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "", temp, 255, path);
return temp.ToString();
}
}
}
Usage method
When used later , We only need to instantiate one IniFile object , You can read and write through the methods in this object INI The configuration file .
Read INI Values in the configuration file
IniFile ini = new IniFile("C://config.ini");
BucketName = ini.IniReadValue("operatorinformation","bucket");
OperatorName = ini.IniReadValue("operatorinformation", "operatorname");
OperatorPwd = ini.IniReadValue("operatorinformation", "operatorpwd");
Write value to INI In profile
IniFile ini = new IniFile("C://config.ini");
ini.IniWriteValue("operatorinformation", "bucket", BucketName);
ini.IniWriteValue("operatorinformation", "operatorname", OperatorName);
ini.IniWriteValue("operatorinformation", "operatorpwd", OperatorPwd);
C# Key value pair operation
using System;
using System.Collections.Generic;
namespace _09 Key value pair
{
class Program
{
static void Main(string[] args)
{
//Dictionary
// Define a set of key value pairs
Dictionary<string, string> dictionary = new Dictionary<string, string>();
// Add key value pair data , The key must be unique , The value is repeatable
dictionary.Add("1", " Zhang Shan ");
dictionary.Add("2", " Li Si ");
dictionary.Add("3", " Wang Wu ");
dictionary.Add("4", " bastard ");
// Reassignment
dictionary["3"] = " Shen Jihan ";
// Determine whether the set contains a key ContainsKey()
if (!dictionary.ContainsKey("5"))
{
dictionary.Add("5", " Yang2 guo4 ");// If not, add
}
else
{
dictionary["5"] = " Yang2 guo4 ";// Include, then change
}
Console.WriteLine(dictionary["5"]);
// use foreach
// Traverse the collection by key
foreach (string item in dictionary.Keys)
{
Console.WriteLine(" key --{0} value --{1}", item, dictionary[item]);
}
// Traverse the collection through key value pairs
foreach (KeyValuePair<string, string> kv in dictionary)
{
Console.WriteLine(" key --{0} value --{1}", kv.Key, kv.Value);
}
Console.ReadKey();
}
}
}
边栏推荐
- Leetcode 0143. rearrange linked list
- 多所“双一流”大学,保研预报名启动!
- Forage QR code -- online QR code generator
- Open source project - taier1.2 release, new workflow, tenant binding simplification and other functions
- Factory mode and constructor mode
- 2022年安全员-A证操作证考试题库模拟考试平台操作
- Custom Configuration Sections
- [try to hack] hfish honeypot deployment
- AFNetworking速成教程
- 卡方分布和伽马函数(Chi-Square Distribution)
猜你喜欢

Revised version | target detection: speed and accuracy comparison (faster r-cnn, r-fcn, SSD, FPN, retinanet and yolov3)

Metersphere -- Open Source continuous testing platform

HCIP第十二天

Daily question - Scholarship

Many "double first-class" universities have launched the research guarantee and prediction name!

How did Dongguan Huawei cloud data center become a new model of green data center?

用友BIP CRM新品发布,赋能大中型企业营销增长

如何只降3D相机不降UI相机的分辨率

Forage QR code -- online QR code generator

【七夕】七夕孤寡小青蛙究极版?七夕节最终章!
随机推荐
ZABBIX distributed
[server data recovery] HP StorageWorks series server RAID5 offline data recovery of two disks
2022 low voltage electrician examination questions and answers
[ecmascript6] iterator and generator
[线程安全问题] 多线程到底可能会带来哪些风险?
Discrete logarithm problem (DLP) & Diffie Hellman problem (DHP)
Excel VBA 开发过程中遇到的一些问题,解决方案,持续更新
草料二维码--在线二维码生成器
Node文件操作
2022 high altitude installation, maintenance, removal of examination question bank and online simulated examination
围绕新市民金融聚焦差异化产品设计、智能技术提效及素养教育
2022年熔化焊接与热切割考题及在线模拟考试
Mobile phone scrolling screenshot software recommendation
文件批量重命名工具Bulk Rename Utility
2022年熔化焊接与热切割考题及在线模拟考试
@DS('slave') 多数据源兼容事务问题解决方案
How to effectively conduct the review meeting (Part 1)?
Force deduction solution summary 1331 array sequence number conversion
zabbix分布式
LeetCode 0142.环形链表 II