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


本文摘自PHP中文网,作者黄舟,侵删。

这篇文章主要为大家详细介绍了C#操作INI配置文件示例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

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

创建如图所示的控件:

源代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

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 = "";

    }

 

  }

 

}

运行结果:

阅读剩余部分

相关阅读 >>

linux下搭建.net core环境方法步骤

.net core中如何使用ref和span<t>提高程序性能的实现代码

c#中关于list<t>的并集与交集以及差集解析

.net?framework?4.5?五个很棒的特性分享

.net core 2.0 preview2的详细介绍

c#中的正则表达式如何验证中文字符的实例

c#中强制转换与尝试转换的实现方法

文件事物管理transactional file manager的实例详解

比较.net中接口与类

详细分析 .net core 构成体系(图)

更多相关阅读请进入《csharp》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...