Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to read and write ini files in C #

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to read and write ini files in C#. It has certain reference value. Interested friends can refer to it. I hope you can learn a lot after reading this article. Let's take a look at it.

1. Create a C# project 2. Create an ini file

Create an ini file in the Debug directory, write the following, and note that the encoding format is ANSI.

[Information]

Name= Zhou Xing

Gender= male

Age=55

Region= Hong Kong

3. Create a winform interface

The figure below is as follows

4. Add an ini management class using System;using System.Collections.Generic;using System.Linq;using System.Runtime.InteropServices;using System.Text;using System.Threading.Tasks Namespace Test1 {public static class IniFunc {/ get the value / segment name / key name / / read the default value of the exception is / the value corresponding to the key name Did not find the full path to return null / the size allowed for the ini file / [DllImport ("kernel32.dll")] private static extern int GetPrivateProfileString (string section, string key, string defval, StringBuilder retval, int size, string filepath) / / write / / write / / paragraph name to be written / / key name to be written / / write value / full path to the ini file / [DllImport ("kernel32.dll")] private static extern int WritePrivateProfileString (string section String key, string val, string filepath) / get data / key name / / default value returned if not found / full path of ini file / public static string getString (string section, string key, string def, string filename) {StringBuilder sb = new StringBuilder (1024) GetPrivateProfileString (section, key, def, sb, 1024, filename); return sb.ToString () } / write data / Segment name / / key name / / write value / ini file full path public static void writeString (string section, string key, string val, string filename) {WritePrivateProfileString (section, key, val, filename) } 5. Add winform code

Double-click the winform interface and add the following code

Using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms; namespace Test1 {public partial class Form1: Form {public Form1 () {InitializeComponent ();} private string filename = null Private void Form1_Load (object sender, EventArgs e) {filename = Application.StartupPath + "\\ Config.ini" } / read / private void Button_Read_Click (object sender, EventArgs e) {string names = IniFunc.getString ("Information", "Name", null, filename); string gender = IniFunc.getString ("Information", "Gender", null, filename) String age = IniFunc.getString ("Information", "Age", null, filename); string region = IniFunc.getString ("Information", "Region", null, filename); TextBox_Name.Text = names; TextBox_Gender.Text = gender; TextBox_Age.Text = age; TextBox_Region.Text = region } / write / private void Button_Write_Click (object sender, EventArgs e) {string names = TextBox_Name.Text; string gender = TextBox_Gender.Text; string age = TextBox_Age.Text; string region = TextBox_Region.Text IniFunc.writeString ("Information", "Name", names, filename); IniFunc.writeString ("Information", "Gender", gender, filename); IniFunc.writeString ("Information", "Age", age, filename); IniFunc.writeString ("Information", "Region", region, filename) } / clear / private void Button_Clear_Click (object sender, EventArgs e) {TextBox_Name.Text = string.Empty; TextBox_Gender.Text = string.Empty; TextBox_Age.Text = string.Empty TextBox_Region.Text = string.Empty;} Thank you for reading this article carefully. I hope the article "how to read and write ini documents in C#" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report