C#中INI設定檔的圖文代碼詳解

來源:互聯網
上載者:User
這篇文章主要為大家詳細介紹了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 = "";    }  }}

運行結果:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.