目录
实践过程
效果
代码
public partial class Form : Form
{
public Form()
{
InitializeComponent();
}
[DllImport("kernel")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
public static void IniWriteValue(string section, string key, string value, string path)
{
WritePrivateProfileString(section, key, value, path);
}
string strg;
private void Form_Load(object sender, EventArgs e)
{
strg = Application.StartupPath.ToString();
strg = strg.Substring(, strg.LastIndexOf("\\"));
strg = strg.Substring(, strg.LastIndexOf("\\"));
strg += @"\Test.ini";
}
private void button_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button_Click(object sender, EventArgs e)
{
if (textBox.Text != "" && textBox2.Text != "" && textBox3.Text != "")
{
IniWriteValue(textBox.Text.Trim(),textBox2.Text.Trim(),textBox3.Text.Trim(),strg);
MessageBox.Show("写入成功");
}
}
}
partial class Form
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
private void InitializeComponent()
{
this.button = new System.Windows.Forms.Button();
this.label = new System.Windows.Forms.Label();
this.textBox = new System.Windows.Forms.TextBox();
this.textBox = new System.Windows.Forms.TextBox();
this.label = new System.Windows.Forms.Label();
this.textBox = new System.Windows.Forms.TextBox();
this.label = new System.Windows.Forms.Label();
this.button = new System.Windows.Forms.Button();
this.SuspendLayout();
this.button.Location = new System.Drawing.Point(23, 161);
this.button.Name = "button1";
this.button.Size = new System.Drawing.Size(75, 23);
this.button.TabIndex = 0;
this.button.Text = "写入";
this.button.UseVisualStyleBackColor = true;
this.button.Click += new System.EventHandler(this.button1_Click);
this.label.AutoSize = true;
this.label.Location = new System.Drawing.Point(21, 25);
this.label.Name = "label1";
this.label.Size = new System.Drawing.Size(53, 12);
this.label.TabIndex = 1;
this.label.Text = "节 点:";
this.textBox.Location = new System.Drawing.Point(97, 22);
this.textBox.Name = "textBox1";
this.textBox.Size = new System.Drawing.Size(100, 21);
this.textBox.TabIndex = 2;
this.textBox.Location = new System.Drawing.Point(97, 63);
this.textBox.Name = "textBox2";
this.textBox.Size = new System.Drawing.Size(100, 21);
this.textBox.TabIndex = 4;
this.label.AutoSize = true;
this.label.Location = new System.Drawing.Point(21, 66);
this.label.Name = "label2";
this.label.Size = new System.Drawing.Size(53, 12);
this.label.TabIndex = 3;
this.label.Text = "属 性:";
this.textBox.Location = new System.Drawing.Point(97, 104);
this.textBox.Name = "textBox3";
this.textBox.Size = new System.Drawing.Size(100, 21);
this.textBox.TabIndex = 6;
this.label.AutoSize = true;
this.label.Location = new System.Drawing.Point(21, 107);
this.label.Name = "label3";
this.label.Size = new System.Drawing.Size(53, 12);
this.label.TabIndex = 5;
this.label.Text = "属性值:";
this.button.Location = new System.Drawing.Point(104, 161);
this.button.Name = "button2";
this.button.Size = new System.Drawing.Size(75, 23);
this.button.TabIndex = 7;
this.button.Text = "取消";
this.button.UseVisualStyleBackColor = true;
this.button.Click += new System.EventHandler(this.button2_Click);
this.AutoScaleDimensions = new System.Drawing.SizeF(F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(, 206);
this.Controls.Add(this.button);
this.Controls.Add(this.textBox);
this.Controls.Add(this.label);
this.Controls.Add(this.textBox);
this.Controls.Add(this.label);
this.Controls.Add(this.textBox);
this.Controls.Add(this.label);
this.Controls.Add(this.button);
this.Name = "Form";
this.Text = "Form";
this.Load += new System.EventHandler(this.Form_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button;
private System.Windows.Forms.Label label;
private System.Windows.Forms.TextBox textBox;
private System.Windows.Forms.TextBox textBox;
private System.Windows.Forms.Label label;
private System.Windows.Forms.TextBox textBox;
private System.Windows.Forms.Label label;
private System.Windows.Forms.Button button;
}
public partial class Form : Form
{
public Form()
{
InitializeComponent();
}
string strg;
[DllImport("kernel")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retval, int size, string filePath);
public static string IniReadValue(string section, string key, string path)
{
StringBuilder temp = new StringBuilder();
int i = GetPrivateProfileString(section, key, "", temp,, path);
return temp.ToString();
}
private void Form_Load(object sender, EventArgs e)
{
strg = Application.StartupPath.ToString();
strg = strg.Substring(, strg.LastIndexOf("\\"));
strg = strg.Substring(, strg.LastIndexOf("\\"));
strg += @"\Test.ini";
}
private void button_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button_Click(object sender, EventArgs e)
{
textBox.Text = IniReadValue(textBox1.Text.Trim(),textBox2.Text.Trim(),strg);
}
}
partial class Form
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
private void InitializeComponent()
{
this.button = new System.Windows.Forms.Button();
this.textBox = new System.Windows.Forms.TextBox();
this.label = new System.Windows.Forms.Label();
this.textBox = new System.Windows.Forms.TextBox();
this.label = new System.Windows.Forms.Label();
this.textBox = new System.Windows.Forms.TextBox();
this.label = new System.Windows.Forms.Label();
this.button = new System.Windows.Forms.Button();
this.SuspendLayout();
this.button.Location = new System.Drawing.Point(101, 153);
this.button.Name = "button2";
this.button.Size = new System.Drawing.Size(75, 23);
this.button.TabIndex = 15;
this.button.Text = "取消";
this.button.UseVisualStyleBackColor = true;
this.button.Click += new System.EventHandler(this.button2_Click);
this.textBox.Location = new System.Drawing.Point(94, 96);
this.textBox.Name = "textBox3";
this.textBox.Size = new System.Drawing.Size(100, 21);
this.textBox.TabIndex = 14;
this.label.AutoSize = true;
this.label.Location = new System.Drawing.Point(18, 99);
this.label.Name = "label3";
this.label.Size = new System.Drawing.Size(53, 12);
this.label.TabIndex = 13;
this.label.Text = "属性值:";
this.textBox.Location = new System.Drawing.Point(94, 55);
this.textBox.Name = "textBox2";
this.textBox.Size = new System.Drawing.Size(100, 21);
this.textBox.TabIndex = 12;
this.label.AutoSize = true;
this.label.Location = new System.Drawing.Point(18, 58);
this.label.Name = "label2";
this.label.Size = new System.Drawing.Size(53, 12);
this.label.TabIndex = 11;
this.label.Text = "属 性:";
this.textBox.Location = new System.Drawing.Point(94, 14);
this.textBox.Name = "textBox1";
this.textBox.Size = new System.Drawing.Size(100, 21);
this.textBox.TabIndex = 10;
this.label.AutoSize = true;
this.label.Location = new System.Drawing.Point(18, 17);
this.label.Name = "label1";
this.label.Size = new System.Drawing.Size(53, 12);
this.label.TabIndex = 9;
this.label.Text = "节 点:";
this.button.Location = new System.Drawing.Point(20, 153);
this.button.Name = "button1";
this.button.Size = new System.Drawing.Size(75, 23);
this.button.TabIndex = 8;
this.button.Text = "读取";
this.button.UseVisualStyleBackColor = true;
this.button.Click += new System.EventHandler(this.button1_Click);
this.AutoScaleDimensions = new System.Drawing.SizeF(F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(, 200);
this.Controls.Add(this.button);
this.Controls.Add(this.textBox);
this.Controls.Add(this.label);
this.Controls.Add(this.textBox);
this.Controls.Add(this.label);
this.Controls.Add(this.textBox);
this.Controls.Add(this.label);
this.Controls.Add(this.button);
this.Name = "Form";
this.Text = "Form";
this.Load += new System.EventHandler(this.Form_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button;
private System.Windows.Forms.TextBox textBox;
private System.Windows.Forms.Label label;
private System.Windows.Forms.TextBox textBox;
private System.Windows.Forms.Label label;
private System.Windows.Forms.TextBox textBox;
private System.Windows.Forms.Label label;
private System.Windows.Forms.Button button;
}