欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2017年07月13日 11:41:41   作者:cnc  
這篇文章主要為大家詳細(xì)介紹了C#操作INI配置文件示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了C#操作INI配置文件示例的具體代碼,供大家參考,具體內(nèi)容如下

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

創(chuàng)建如圖所示的控件:

源代碼:

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("姓名或地區(qū)不能為空!", "錯誤", 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 = "";
    }

  }

} 

 運(yùn)行結(jié)果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C#讀寫xml文件方法總結(jié)(超詳細(xì)!)

    C#讀寫xml文件方法總結(jié)(超詳細(xì)!)

    項目中用到關(guān)于xml文件讀寫操,所以下面這篇文章主要給大家介紹了關(guān)于C#讀寫xml文件方法的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-02-02
  • C#中的應(yīng)用程序接口介紹及實(shí)現(xiàn),密封類與密封方法

    C#中的應(yīng)用程序接口介紹及實(shí)現(xiàn),密封類與密封方法

    今天小編就為大家分享一篇關(guān)于C#中的應(yīng)用程序接口介紹及實(shí)現(xiàn),密封類與密封方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-10-10
  • C#中使用HttpPost調(diào)用WebService的方法

    C#中使用HttpPost調(diào)用WebService的方法

    這篇文章介紹了C#中使用HttpPost調(diào)用WebService的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-03-03
  • C#算法之無重復(fù)字符的最長子串

    C#算法之無重復(fù)字符的最長子串

    這篇文章介紹了C#算法之無重復(fù)字符的最長子串,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-01-01
  • C#導(dǎo)出文本內(nèi)容到word文檔的方法

    C#導(dǎo)出文本內(nèi)容到word文檔的方法

    這篇文章主要介紹了C#導(dǎo)出文本內(nèi)容到word文檔的方法,涉及C#操作word文檔的相關(guān)技巧,需要的朋友可以參考下
    2015-04-04
  • C#使用Socket實(shí)現(xiàn)通信的方法示例

    C#使用Socket實(shí)現(xiàn)通信的方法示例

    這篇文章主要介紹了C#使用Socket實(shí)現(xiàn)通信的方法示例,文章按照 Socket 的 創(chuàng)建、連接、傳輸數(shù)據(jù)、釋放資源的過程來寫,給出方法、參數(shù)的詳細(xì)信息,文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下
    2024-06-06
  • C#中使用?record?的好處和最佳場景

    C#中使用?record?的好處和最佳場景

    這篇文章主要介紹了C#中使用?record?的好處,使用?record?類型的主要好處包括簡潔的語法、自動生成的成員、基于值的相等性、非破壞性復(fù)制、解構(gòu)支持、繼承支持和與模式匹配的良好集成,需要的朋友可以參考下
    2024-07-07
  • C#中如何利用正則表達(dá)式判斷字符

    C#中如何利用正則表達(dá)式判斷字符

    這篇文章主要介紹了C#中利用正則表達(dá)式判斷字符的實(shí)例代碼,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-12-12
  • 最新評論