您的位置:首页 > 编程语言 > C#

C#读写INI文件

2005-04-20 16:48 477 查看
1

using System;
2

using System.Drawing;
3

using System.Collections;
4

using System.ComponentModel;
5

using System.Windows.Forms;
6

using System.IO;
7

using System.Runtime.InteropServices;
8

using System.Text;
9

namespace ini
10



...{
11



/**//// <summary>
12

/// Form1 的摘要说明。
13

/// </summary>
14

public class Form1 : System.Windows.Forms.Form
15



...{
16

private System.Windows.Forms.Button button1;
17

private System.Windows.Forms.TextBox textBox1;
18

private System.Windows.Forms.TextBox textBox2;
19

private System.Windows.Forms.TextBox textBox3;
20

private System.Windows.Forms.Button button3;
21

public string database,userid,server;
22

//申明INI文件的写操作函数WritePrivateProfileString()
23

[ DllImport ( "kernel32" ) ]
24

private static extern long WritePrivateProfileString ( string section , string key , string val , string filePath ) ;
25

//申明INI文件的读操作函数GetPrivateProfileString()
26

[ DllImport ( "kernel32" ) ]
27

private static extern int GetPrivateProfileString ( string section ,string key , string def , StringBuilder retVal , int size , string filePath ) ;
28


29



/**//// <summary>
30

/// 必需的设计器变量。
31

/// </summary>
32

private System.ComponentModel.Container components = null;
33


34

public Form1()
35



...{
36

//
37

// Windows 窗体设计器支持所必需的
38

//
39

InitializeComponent();
40


41

//
42

// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
43

//
44

}
45


46



/**//// <summary>
47

/// 清理所有正在使用的资源。
48

/// </summary>
49

protected override void Dispose( bool disposing )
50



...{
51

if( disposing )
52



...{
53

if (components != null)
54



...{
55

components.Dispose();
56

}
57

}
58

base.Dispose( disposing );
59

}
60


61



Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
62



/**//// <summary>
63

/// 设计器支持所需的方法 - 不要使用代码编辑器修改
64

/// 此方法的内容。
65

/// </summary>
66

private void InitializeComponent()
67



...{
68

this.button1 = new System.Windows.Forms.Button();
69

this.textBox1 = new System.Windows.Forms.TextBox();
70

this.textBox2 = new System.Windows.Forms.TextBox();
71

this.textBox3 = new System.Windows.Forms.TextBox();
72

this.button3 = new System.Windows.Forms.Button();
73

this.SuspendLayout();
74

//
75

// button1
76

//
77

this.button1.Location = new System.Drawing.Point(176, 16);
78

this.button1.Name = "button1";
79

this.button1.Size = new System.Drawing.Size(88, 24);
80

this.button1.TabIndex = 0;
81

this.button1.Text = "写ini文件";
82

this.button1.Click += new System.EventHandler(this.button1_Click);
83

//
84

// textBox1
85

//
86

this.textBox1.Location = new System.Drawing.Point(40, 8);
87

this.textBox1.Name = "textBox1";
88

this.textBox1.Size = new System.Drawing.Size(96, 21);
89

this.textBox1.TabIndex = 1;
90

this.textBox1.Text = "";
91

//
92

// textBox2
93

//
94

this.textBox2.Location = new System.Drawing.Point(40, 40);
95

this.textBox2.Name = "textBox2";
96

this.textBox2.Size = new System.Drawing.Size(96, 21);
97

this.textBox2.TabIndex = 2;
98

this.textBox2.Text = "";
99

//
100

// textBox3
101

//
102

this.textBox3.Location = new System.Drawing.Point(40, 72);
103

this.textBox3.Name = "textBox3";
104

this.textBox3.Size = new System.Drawing.Size(96, 21);
105

this.textBox3.TabIndex = 3;
106

this.textBox3.Text = "";
107

//
108

// button3
109

//
110

this.button3.Location = new System.Drawing.Point(176, 56);
111

this.button3.Name = "button3";
112

this.button3.Size = new System.Drawing.Size(88, 24);
113

this.button3.TabIndex = 6;
114

this.button3.Text = "读ini文件";
115

this.button3.Click += new System.EventHandler(this.button3_Click);
116

//
117

// Form1
118

//
119

this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
120

this.ClientSize = new System.Drawing.Size(320, 117);
121

this.Controls.Add(this.button3);
122

this.Controls.Add(this.textBox3);
123

this.Controls.Add(this.textBox2);
124

this.Controls.Add(this.textBox1);
125

this.Controls.Add(this.button1);
126

this.Name = "Form1";
127

this.Text = "ini";
128

this.Load += new System.EventHandler(this.Form1_Load);
129

this.ResumeLayout(false);
130


131

}
132

#endregion
133


134



/**//// <summary>
135

/// 应用程序的主入口点。
136

/// </summary>
137

[STAThread]
138

static void Main()
139



...{
140

Application.Run(new Form1());
141

}
142

private void Server_Info()
143



...{
144

string s = Application.ExecutablePath;
145

string s1;
146

s1 = s.Replace("ini.exe","config.ini");
147

//写入INI 文件
148

string FileName = "FiatDataBase" ;
149

string path = s1;
150

string key1 = "DataBase";
151

string keyValue1 = textBox1.Text ;
152

string key2 = "Server";
153

string keyValue2 = textBox2.Text ;
154

string key3 = "UserId";
155

string keyValue3 = textBox3.Text ;
156

WritePrivateProfileString(FileName,key1,keyValue1,path) ;
157

WritePrivateProfileString(FileName,key2,keyValue2,path) ;
158

WritePrivateProfileString(FileName,key3,keyValue3,path) ;
159

MessageBox.Show("完毕!");
160

}
161

//读取服务器信息
162


163


164

private void button1_Click(object sender, System.EventArgs e)
165



...{
166

Server_Info();
167


168

}
169


170

private void button2_Click(object sender, System.EventArgs e)
171



...{
172


173


174

}
175


176

//读取服务器信息
177

private void Read_SystemInfo()
178



...{
179

try
180



...{
181

//获取配置信息
182

string s = Application.ExecutablePath;
183

string s1;
184

s1 = s.Replace("ini.exe","config.ini");
185

StringBuilder temp1 = new StringBuilder ( 255 ) ;
186

int i1 = GetPrivateProfileString ("FiatDataBase" ,"DataBase" , "" , temp1 , 255 , s1 ) ;
187

database = temp1.ToString();
188

StringBuilder temp2 = new StringBuilder ( 255 ) ;
189

int i2 = GetPrivateProfileString ("FiatDataBase" ,"Server" , "" , temp2 , 255 , s1 ) ;
190

server = temp2.ToString();
191

StringBuilder temp3 = new StringBuilder ( 255 ) ;
192

int i3 = GetPrivateProfileString ("FiatDataBase" ,"UserId" , "" , temp3 , 255 , s1 ) ;
193

userid = temp3.ToString();
194

}
195

catch(Exception e1)
196



...{
197

MessageBox.Show("请先配置服务器信息!");
198

}
199

}
200


201

private void button3_Click(object sender, System.EventArgs e)
202



...{
203


204

Read_SystemInfo();
205


206

}
207


208

private void Form1_Load(object sender, System.EventArgs e)
209



...{
210


211

}
212

}
213

}
214


215


216
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: