国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#操作INI配置文件示例詳解

2020-01-24 00:34:39
字體:
來源:轉載
供稿:網友

本文實例為大家分享了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 + "http://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 = "";    }  }} 

 運行結果:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 拜泉县| 通城县| 浙江省| 太谷县| 镇雄县| 咸宁市| 黄浦区| 武隆县| 康保县| 洛宁县| 荔浦县| 成安县| 白山市| 东乡县| 丰都县| 蒲江县| 兴安县| 南皮县| 井研县| 新巴尔虎左旗| 沾化县| 厦门市| 石屏县| 收藏| 全州县| 南华县| 仪陇县| 贵州省| 高邑县| 翼城县| 巴青县| 许昌县| 合作市| 星座| 合水县| 山东省| 大港区| 贵阳市| 广水市| 淄博市| 黑河市|