首页后端开发ASP.NETC#中INI配置文件的图文代码详解

C#中INI配置文件的图文代码详解

时间2024-01-30 12:43:02发布访客分类ASP.NET浏览233
导读:收集整理的这篇文章主要介绍了C#中INI配置文件的图文代码详解,觉得挺不错的,现在分享给大家,也给大家做个参考。这篇文章主要为大家详细介绍了C#操作INI配置文件示例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下本文实例为大家分享了C#...
收集整理的这篇文章主要介绍了C#中INI配置文件的图文代码详解,觉得挺不错的,现在分享给大家,也给大家做个参考。这篇文章主要为大家详细介绍了C#操作INI配置文件示例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C#操作INI配置文件示例的具体代码,供大家参考,具体内容如下

源文件地址:C#操作INI配置文件示例

创建如图所示的控件:

源代码:

using System;
    using System.Collections.Generic;
    using System.componentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
namespace WindowsFormsApplication3{
  public partial class Form1 : Form  {
    public Form1()    {
          InITializeComponent();
    }
         [DllImport("kernel32.dll")]    PRivate static extern long WritePrivateProfileString(string section, string key, string value, string filepath);
         [DllImport("kernel32.dll")]    private static extern int GetPrivateProfileString(string section,string key,string def,StringBuilder returnvalue,intbuffersize,string filepath);
         private string IniFilePath;
    private void Form1_Load(object sender, Eventargs e)    {
          comboBox1.Text = "男";
          for (int i = 1;
     i = 100;
 i++)      {
            comboBox2.Items.Add(i.ToString());
      }
          comboBox2.Text = "18";
          IniFilePath = Application.StartupPath + "\\config.ini";
    }
     private void button1_Click(object sender, EventArgs e)    {
          if ((textBox1.Text.Trim() != "") &
    &
 (textBox2.Text.Trim() != ""))      {
            string Section = "Information";
        try        {
              WritePrivateProfileString(Section, "Name", textBox1.Text.Trim(), IniFilePath);
              WritePrivateProfileString(Section, "Gender", comboBox1.Text, IniFilePath);
              WritePrivateProfileString(Section, "Age", comboBox2.Text, IniFilePath);
              WritePrivateProfileString(Section, "Region", textBox2.Text.Trim(), IniFilePath);
        }
        catch (Exception ee)        {
              MessageBox.Show(ee.Message);
        }
      }
      else      {
            MessageBox.Show("姓名或地区不能为空!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
      }
    }
     private void button2_Click(object sender, EventArgs e)    {
          string outString;
      try      {
            GetValue("Information", "Name", out outString);
            textBox1.Text = outString;
            GetValue("Information", "Gender", out outString);
            comboBox1.Text = outString;
            GetValue("Information", "Age", out outString);
            comboBox2.Text = outString;
            GetValue("Information", "Region", out outString);
            textBox2.Text = outString;
      }
      catch (Exception ee)      {
            MessageBox.Show(ee.Message);
      }
     }
     private void GetValue(string section,string key, out string value)    {
          StringBuilder stringBuilder = new StringBuilder();
          GetPrivateProfileString(section, key, "", stringBuilder, 1024, IniFilePath);
          value = stringBuilder.ToString();
    }
     private void button3_Click(object sender, EventArgs e)    {
          textBox1.Text = "";
          comboBox1.Text = "男";
          comboBox2.Text = "18";
          textBox2.Text = "";
    }
  }
}
    

运行结果:

以上就是C#中INI配置文件的图文代码详解的详细内容,更多请关注其它相关文章!

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!

csharp配置文件

若转载请注明出处: C#中INI配置文件的图文代码详解
本文地址: https://pptw.com/jishu/592535.html
手把手教你怎么正确快速的部署一个TS Node.js项目! C#中关于TCP粘包出现的问题解决的示例

游客 回复需填写必要信息