2012年3月19日星期一

使用C#操作INI文件

Frm_Main.cs

View Code
 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.IO;
10 using System.Runtime.InteropServices;
11
12 namespace INIFileOperate
13 {
14 public partial class Frm_Main : Form
15 {
16 public Frm_Main()
17 {
18 InitializeComponent();
19 }
20 #region 变量声明区
21 public string str = "";//该变量保存INI文件所在的具体物理位置
22 public string strOne = "";
23 [DllImport("kernel32")]
24 private static extern int GetPrivateProfileString(
25 string lpAppName,
26 string lpKeyName,
27 string lpDefault,
28 StringBuilder lpReturnedString,
29 int nSize,
30 string lpFileName);
31
32 public string ContentReader(string area, string key, string def)
33 {
34 StringBuilder stringBuilder = new StringBuilder(1024); //定义一个最大长度为1024的可变字符串
35 GetPrivateProfileString(area, key, def, stringBuilder, 1024, str); //读取INI文件
36 return stringBuilder.ToString(); //返回INI文件的内容
37 }
38
39 [DllImport("kernel32")]
40 private static extern long WritePrivateProfileString(
41 string mpAppName,
42 string mpKeyName,
43 string mpDefault,
44 string mpFileName);
45
46 #endregion
47
48 #region 窗体加载部分
49 private void Form1_Load(object sender, EventArgs e)
50 {
51 str = Application.StartupPath + "\\ConnectString.ini"; //INI文件的物理地址
52 strOne = System.IO.Path.GetFileNameWithoutExtension(str); //获取INI文件的文件名
53 if (File.Exists(str)) //判断是否存在该INI文件
54 {
55 server.Text = ContentReader(strOne, "Data Source", ""); //读取INI文件中服务器节点的内容
56 database.Text = ContentReader(strOne, "DataBase", ""); //读取INI文件中数据库节点的内容
57 uid.Text = ContentReader(strOne, "Uid", ""); //读取INI文件中用户节点的内容
58 pwd.Text = ContentReader(strOne, "Pwd", ""); //读取INI文件中密码节点的内容
59 }
60 }
61 #endregion
62
63 #region 进行修改操作
64 private void button1_Click(object sender, EventArgs e)
65 {
66 if (File.Exists(str)) //判断是否存在INI文件
67 {
68 WritePrivateProfileString(strOne, "Data Source", server.Text, str); //修改INI文件中服务器节点的内容
69 WritePrivateProfileString(strOne, "DataBase", database.Text, str); //修改INI文件中数据库节点的内容
70 WritePrivateProfileString(strOne, "Uid", uid.Text, str); //修改INI文件中用户节点的内容
71 WritePrivateProfileString(strOne, "Pwd", pwd.Text, str); //修改INI文件中密码节点的内容
72 MessageBox.Show("恭喜你,修改成功!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
73 }
74 else
75 {
76 MessageBox.Show("对不起,你所要修改的文件不存在,请确认后再进行修改操作!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
77 }
78 }
79 #endregion
80 }
81 }

Frm_Main.designer.cs

View Code
  1 namespace INIFileOperate
2 {
3 partial class Frm_Main
4 {
5 /// <summary>
6 /// 必需的设计器变量。
7 /// </summary>
8 private System.ComponentModel.IContainer components = null;
9
10 /// <summary>
11 /// 清理所有正在使用的资源。
12 /// </summary>
13 /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
14 protected override void Dispose(bool disposing)
15 {
16 if (disposing && (components != null))
17 {
18 components.Dispose();
19 }
20 base.Dispose(disposing);
21 }
22
23 #region Windows 窗体设计器生成的代码
24
25 /// <summary>
26 /// 设计器支持所需的方法 - 不要
27 /// 使用代码编辑器修改此方法的内容。
28 /// </summary>
29 private void InitializeComponent()
30 {
31 this.groupBox1 = new System.Windows.Forms.GroupBox();
32 this.label1 = new System.Windows.Forms.Label();
33 this.label2 = new System.Windows.Forms.Label();
34 this.pwd = new System.Windows.Forms.TextBox();
35 this.label8 = new System.Windows.Forms.Label();
36 this.uid = new System.Windows.Forms.TextBox();
37 this.label9 = new System.Windows.Forms.Label();
38 this.server = new System.Windows.Forms.TextBox();
39 this.database = new System.Windows.Forms.TextBox();
40 this.button1 = new System.Windows.Forms.Button();
41 this.groupBox1.SuspendLayout();
42 this.SuspendLayout();
43 //
44 // groupBox1
45 //
46 this.groupBox1.Controls.Add(this.label1);
47 this.groupBox1.Controls.Add(this.label2);
48 this.groupBox1.Controls.Add(this.pwd);
49 this.groupBox1.Controls.Add(this.label8);
50 this.groupBox1.Controls.Add(this.uid);
51 this.groupBox1.Controls.Add(this.label9);
52 this.groupBox1.Controls.Add(this.server);
53 this.groupBox1.Controls.Add(this.database);
54 this.groupBox1.Location = new System.Drawing.Point(4, 7);
55 this.groupBox1.Name = "groupBox1";
56 this.groupBox1.Size = new System.Drawing.Size(177, 126);
57 this.groupBox1.TabIndex = 39;
58 this.groupBox1.TabStop = false;
59 this.groupBox1.Text = "INI文件";
60 //
61 // label1
62 //
63 this.label1.AutoSize = true;
64 this.label1.Location = new System.Drawing.Point(6, 18);
65 this.label1.Name = "label1";
66 this.label1.Size = new System.Drawing.Size(53, 12);
67 this.label1.TabIndex = 17;
68 this.label1.Text = "服务器:";
69 //
70 // label2
71 //
72 this.label2.AutoSize = true;
73 this.label2.Location = new System.Drawing.Point(6, 44);
74 this.label2.Name = "label2";
75 this.label2.Size = new System.Drawing.Size(53, 12);
76 this.label2.TabIndex = 16;
77 this.label2.Text = "数据库:";
78 //
79 // pwd
80 //
81 this.pwd.Location = new System.Drawing.Point(70, 94);
82 this.pwd.Name = "pwd";
83 this.pwd.Size = new System.Drawing.Size(100, 21);
84 this.pwd.TabIndex = 26;
85 //
86 // label8
87 //
88 this.label8.AutoSize = true;
89 this.label8.Location = new System.Drawing.Point(5, 72);
90 this.label8.Name = "label8";
91 this.label8.Size = new System.Drawing.Size(65, 12);
92 this.label8.TabIndex = 11;
93 this.label8.Text = "登录身份:";
94 //
95 // uid
96 //
97 this.uid.Location = new System.Drawing.Point(70, 66);
98 this.uid.Name = "uid";
99 this.uid.Size = new System.Drawing.Size(100, 21);
100 this.uid.TabIndex = 24;
101 //
102 // label9
103 //
104 this.label9.AutoSize = true;
105 this.label9.Location = new System.Drawing.Point(5, 100);
106 this.label9.Name = "label9";
107 this.label9.Size = new System.Drawing.Size(65, 12);
108 this.label9.TabIndex = 9;
109 this.label9.Text = "登录密码:";
110 //
111 // server
112 //
113 this.server.Location = new System.Drawing.Point(70, 11);
114 this.server.Name = "server";
115 this.server.Size = new System.Drawing.Size(100, 21);
116 this.server.TabIndex = 13;
117 //
118 // database
119 //
120 this.database.Location = new System.Drawing.Point(70, 38);
121 this.database.Name = "database";
122 this.database.Size = new System.Drawing.Size(100, 21);
123 this.database.TabIndex = 22;
124 //
125 // button1
126 //
127 this.button1.Location = new System.Drawing.Point(187, 110);
128 this.button1.Name = "button1";
129 this.button1.Size = new System.Drawing.Size(55, 23);
130 this.button1.TabIndex = 40;
131 this.button1.Text = "修改";
132 this.button1.UseVisualStyleBackColor = true;
133 this.button1.Click += new System.EventHandler(this.button1_Click);
134 //
135 // Frm_Main
136 //
137 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
138 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
139 this.ClientSize = new System.Drawing.Size(247, 144);
140 this.Controls.Add(this.button1);
141 this.Controls.Add(this.groupBox1);
142 this.Name = "Frm_Main";
143 this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
144 this.Text = "使用C#操作INI文件";
145 this.Load += new System.EventHandler(this.Form1_Load);
146 this.groupBox1.ResumeLayout(false);
147 this.groupBox1.PerformLayout();
148 this.ResumeLayout(false);
149
150 }
151
152 #endregion
153
154 private System.Windows.Forms.GroupBox groupBox1;
155 private System.Windows.Forms.Label label1;
156 private System.Windows.Forms.Label label2;
157 private System.Windows.Forms.TextBox pwd;
158 private System.Windows.Forms.Label label8;
159 private System.Windows.Forms.TextBox uid;
160 private System.Windows.Forms.Label label9;
161 private System.Windows.Forms.TextBox server;
162 private System.Windows.Forms.TextBox database;
163 private System.Windows.Forms.Button button1;
164 }
165 }



作者:墨明棋妙
出处:http://www.cnblogs.com/ynbt/
关于作者:专注于.Net、WCF和移动互联网开发。
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,如有问题,可以通过ynbt_wang@163.com联系我,非常感谢。 。
使用C#操作INI文件

TAG: