C#实现读写ini配置文件的方法详解

.NET
270
0
0
2023-07-04
标签   C#
目录
  • 实践过程
  • 效果
  • 代码

实践过程

效果

代码

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
{
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

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

    /// <summary>
    /// 设计器支持所需的方法 - 不要
    /// 使用代码编辑器修改此方法的内容。
    /// </summary>
    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();
        // 
        // button
        // 
        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);
        // 
        // label
        // 
        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 = "节  点:";
        // 
        // textBox
        // 
        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;
        // 
        // textBox
        // 
        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;
        // 
        // label
        // 
        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 = "属  性:";
        // 
        // textBox
        // 
        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;
        // 
        // label
        // 
        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 = "属性值:";
        // 
        // button
        // 
        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);
        // 
        // Form
        // 
        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
{
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

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

    /// <summary>
    /// 设计器支持所需的方法 - 不要
    /// 使用代码编辑器修改此方法的内容。
    /// </summary>
    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();
        // 
        // button
        // 
        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);
        // 
        // textBox
        // 
        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;
        // 
        // label
        // 
        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 = "属性值:";
        // 
        // textBox
        // 
        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;
        // 
        // label
        // 
        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 = "属  性:";
        // 
        // textBox
        // 
        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;
        // 
        // label
        // 
        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 = "节  点:";
        // 
        // button
        // 
        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);
        // 
        // Form
        // 
        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;
}