当前位置:网站首页>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();
}
}
}
边栏推荐
- 2022 melting welding and thermal cutting examination questions and online simulation examination
- 如何有效进行回顾会议(上)?
- 草料二维码--在线二维码生成器
- 468产品策划与推广方案(150份)
- FormData对象的使用, var formdata=new FormData()
- Niuke multi school link with level editor i- (linear DP)
- [ecmascript6] proxy and reflection
- (function(global,factory){
- Solve the problem that uniapp wechat applet canvas cannot introduce fonts
- RSA encrypts data with private key and decrypts data with public key (not a signature verification process)
猜你喜欢

九、uni-popup用法 下拉框底部弹窗效果

Hcip day 12

Cv:: mat conversion to qimage error

Open source project - taier1.2 release, new workflow, tenant binding simplification and other functions

What is a spin lock? A spin lock means that when a thread attempts to acquire a lock, if the lock has been occupied by other threads, it will always cycle to detect whether the lock has been released,

手机滚动截屏软件推荐
C# 获取当前路径7种方法

开源项目丨Taier1.2版本发布,新增工作流、租户绑定简化等多项功能

LeetCode 0142.环形链表 II

Development and definition of software testing
随机推荐
LeetCode 0142.环形链表 II
[translation] salt companies come to linkerd for load balancing, and stay for efficiency, reliability and performance
【七夕】七夕孤寡小青蛙究极版?七夕节最终章!
LeetCode 1331.数组序号转换
Recommended super easy-to-use mobile screen recording software
Solve the problem that uniapp wechat applet canvas cannot introduce fonts
数据库优化 理解这些就够了
Cv:: mat conversion to qimage error
Minitest -- applet automation testing framework
Discrete logarithm problem (DLP) & Diffie Hellman problem (DHP)
Clickhouse architecture and design
How did Dongguan Huawei cloud data center become a new model of green data center?
3种方法解轮转数组
如何有效进行回顾会议(上)?
Detailed explanation of C language student achievement management system [easy to understand]
Literature reading (245) roller
Collaborative office tools: Online whiteboard is in its infancy, and online design has become a red sea
PowerDesigner creates a database model (conceptual model example)
468产品策划与推广方案(150份)
Revised version | target detection: speed and accuracy comparison (faster r-cnn, r-fcn, SSD, FPN, retinanet and yolov3)