查看: 1904|回复: 0

[ASP.NET教程] C#操作INI配置文件示例详解

发表于 2018-2-10 08:00:07

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

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

创建如图所示的控件:

源代码:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Runtime.InteropServices;
  10. namespace WindowsFormsApplication3
  11. {
  12. public partial class Form1 : Form
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. }
  18. [DllImport("kernel32.dll")]
  19. private static extern long WritePrivateProfileString(string section, string key, string value, string filepath);
  20. [DllImport("kernel32.dll")]
  21. private static extern int GetPrivateProfileString(string section,string key,string def,StringBuilder returnvalue,intbuffersize,string filepath);
  22. private string IniFilePath;
  23. private void Form1_Load(object sender, EventArgs e)
  24. {
  25. comboBox1.Text = "男";
  26. for (int i = 1; i <= 100; i++)
  27. {
  28. comboBox2.Items.Add(i.ToString());
  29. }
  30. comboBox2.Text = "18";
  31. IniFilePath = Application.StartupPath + "\\Config.ini";
  32. }
  33. private void button1_Click(object sender, EventArgs e)
  34. {
  35. if ((textBox1.Text.Trim() != "") && (textBox2.Text.Trim() != ""))
  36. {
  37. string Section = "Information";
  38. try
  39. {
  40. WritePrivateProfileString(Section, "Name", textBox1.Text.Trim(), IniFilePath);
  41. WritePrivateProfileString(Section, "Gender", comboBox1.Text, IniFilePath);
  42. WritePrivateProfileString(Section, "Age", comboBox2.Text, IniFilePath);
  43. WritePrivateProfileString(Section, "Region", textBox2.Text.Trim(), IniFilePath);
  44. }
  45. catch (Exception ee)
  46. {
  47. MessageBox.Show(ee.Message);
  48. }
  49. }
  50. else
  51. {
  52. MessageBox.Show("姓名或地区不能为空!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  53. }
  54. }
  55. private void button2_Click(object sender, EventArgs e)
  56. {
  57. string outString;
  58. try
  59. {
  60. GetValue("Information", "Name", out outString);
  61. textBox1.Text = outString;
  62. GetValue("Information", "Gender", out outString);
  63. comboBox1.Text = outString;
  64. GetValue("Information", "Age", out outString);
  65. comboBox2.Text = outString;
  66. GetValue("Information", "Region", out outString);
  67. textBox2.Text = outString;
  68. }
  69. catch (Exception ee)
  70. {
  71. MessageBox.Show(ee.Message);
  72. }
  73. }
  74. private void GetValue(string section,string key, out string value)
  75. {
  76. StringBuilder stringBuilder = new StringBuilder();
  77. GetPrivateProfileString(section, key, "", stringBuilder, 1024, IniFilePath);
  78. value = stringBuilder.ToString();
  79. }
  80. private void button3_Click(object sender, EventArgs e)
  81. {
  82. textBox1.Text = "";
  83. comboBox1.Text = "男";
  84. comboBox2.Text = "18";
  85. textBox2.Text = "";
  86. }
  87. }
  88. }
复制代码

运行结果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。

您可能感兴趣的文章:

  • C# Winform 调用系统接口操作 INI 配置文件的代码
  • C#中读写INI文件的方法例子
  • C#线程 BeginInvoke和EndInvoke使用方法
  • c#实现ini文件读写类分享
  • C# Ini文件操作实例
  • c#读写ini配置文件示例
  • C#中Invoke 和 BeginInvoke 的真正涵义
  • c# Invoke和BeginInvoke 区别分析
  • C#实现读写ini文件类实例
  • C#实现利用Windows API读写INI文件的方法


回复

使用道具 举报