.NET Logo
Welcome Guest Search | Active Topics | Members | Log In | Register

เทคนิคอ่าน-เขียน ไฟล์ .ini Options · View
AlZheiMeR
Posted: Monday, August 04, 2008 2:19:42 AM
Rank: มือฝึกหัด
Groups: Member

Joined: 7/6/2008
Posts: 5
Location: thailand

มือใหม่ครับ พึ่งเริ่มหัดเขียนโปรแกรม เริ่มจาก C#.net 2008 เลย งงมากมาย

ใครมีเทคนิคดีกว่านี้ หรือผู้รุ้ ช่วยแนะนำวิธีที่ดีกว่านี้ก็ดีนะครับ

อันนี้เป็นไฟล์ AlzINI.cs

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AlzINI
{
    class AlzINI
    {
        #region Private member
        private String FileINI;
        public class KeyINI
        {
            public String Key;
            public String Value;
        }
        List<KeyINI> mKeys = new List<KeyINI>();
        #endregion

        #region Private Function
        private int KeySearch(String s)
        {
            int Search = 0;
            for (int i = 0; i < mKeys.Count; i++)
            {
                if (mKeys[i].Key == s) { Search = i + 1; }
            }
            return Search;
        }
        #endregion

        #region Public member and function
        //Key count
        public int Count;
        //Read("UserID") return Value
        public String ReadKey(String s)
        {
            if (KeySearch(s) != 0)
            {
                return mKeys[KeySearch(s) - 1].Value;
            }
            else { return null; }
        }
        //Write("key","value")
        public void WriteKey(String key, String value)
        {
            if (KeySearch(key) != 0)
            {
                mKeys[KeySearch(key) - 1].Value = value;
            }
            else
            {
                mKeys.Add(new KeyINI { Key = key, Value = value });
                Count++;
            }
        }
        //Save() for save to FileINI
        public void Save()
        {
            StreamWriter sw = new StreamWriter(FileINI);
            String s = "";
            for (int i = 0; i < mKeys.Count; i++)
            {
                s = mKeys[i].Key + "=" + mKeys[i].Value;
                sw.WriteLine(s);
            }
            sw.Close();
        }
        #endregion

        public AlzINI(String fileINI)
        {
            FileINI = fileINI;
            StreamReader sr = new StreamReader(fileINI);
            String ReadLine;
            Count = 0;
            while ((ReadLine = sr.ReadLine()) != null)
            {
                if (ReadLine != "")
                {
                    String[] parts = ReadLine.Split(new Char[] { ',', '=' });
                    mKeys.Add(new KeyINI { Key = parts[0], Value = parts[1] });
                    Count++;
                }
            }
            sr.Close();
        }
    }
}

อันนี้เป็นไฟล์ form1.cs

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;

namespace AlzINI
{
    public partial class Form1 : Form
    {
        AlzINI alzINI = new AlzINI("alz.ini");

        private void LoadINI()
        {
            TextID.Text = alzINI.ReadKey("TextID");
            textPass.Text = alzINI.ReadKey("TextPass");
        }

        public Form1()
        {
            InitializeComponent();
            LoadINI();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textKey.Text != "" && textValue.Text != "")
            {
                alzINI.WriteKey(textKey.Text, textValue.Text);
            }
        }

        private void TextID_TextChanged(object sender, EventArgs e)
        {
            alzINI.WriteKey("TextID", TextID.Text);
        }

        private void textPass_TextChanged(object sender, EventArgs e)
        {
            alzINI.WriteKey("TextPass", textPass.Text);
        }
    }
}

อันนี้เป็น ไฟล์ Form1.Designernamespace AlzINI


{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            alzINI.Save();
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.TextID = new System.Windows.Forms.TextBox();
            this.textPass = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.textValue = new System.Windows.Forms.TextBox();
            this.textKey = new System.Windows.Forms.TextBox();
            this.label4 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.groupBox1.SuspendLayout();
            this.SuspendLayout();
            //
            // TextID
            //
            this.TextID.Location = new System.Drawing.Point(74, 12);
            this.TextID.Name = "TextID";
            this.TextID.Size = new System.Drawing.Size(109, 20);
            this.TextID.TabIndex = 0;
            this.TextID.TextChanged += new System.EventHandler(this.TextID_TextChanged);
            //
            // textPass
            //
            this.textPass.Location = new System.Drawing.Point(74, 38);
            this.textPass.Name = "textPass";
            this.textPass.Size = new System.Drawing.Size(109, 20);
            this.textPass.TabIndex = 1;
            this.textPass.TextChanged += new System.EventHandler(this.textPass_TextChanged);
            //
            // label1
            //
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(12, 15);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(21, 13);
            this.label1.TabIndex = 2;
            this.label1.Text = "ID:";
            //
            // label2
            //
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(12, 41);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(56, 13);
            this.label2.TabIndex = 3;
            this.label2.Text = "Password:";
            //
            // button1
            //
            this.button1.Location = new System.Drawing.Point(12, 146);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 4;
            this.button1.Text = "AddKey";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            //
            // button2
            //
            this.button2.Location = new System.Drawing.Point(108, 146);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 5;
            this.button2.Text = "Exit";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            //
            // groupBox1
            //
            this.groupBox1.Controls.Add(this.textValue);
            this.groupBox1.Controls.Add(this.textKey);
            this.groupBox1.Controls.Add(this.label4);
            this.groupBox1.Controls.Add(this.label3);
            this.groupBox1.Location = new System.Drawing.Point(12, 64);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(171, 76);
            this.groupBox1.TabIndex = 6;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Add Key";
            //
            // textValue
            //
            this.textValue.Location = new System.Drawing.Point(62, 48);
            this.textValue.Name = "textValue";
            this.textValue.Size = new System.Drawing.Size(100, 20);
            this.textValue.TabIndex = 3;
            //
            // textKey
            //
            this.textKey.Location = new System.Drawing.Point(62, 22);
            this.textKey.Name = "textKey";
            this.textKey.Size = new System.Drawing.Size(100, 20);
            this.textKey.TabIndex = 2;
            //
            // label4
            //
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(6, 51);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(37, 13);
            this.label4.TabIndex = 1;
            this.label4.Text = "Value:";
            //
            // label3
            //
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(6, 25);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(28, 13);
            this.label3.TabIndex = 0;
            this.label3.Text = "Key:";
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(193, 181);
            this.Controls.Add(this.groupBox1);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.textPass);
            this.Controls.Add(this.TextID);
            this.Name = "Form1";
            this.Text = "Form1";
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();
        }

        #endregion

        private System.Windows.Forms.TextBox TextID;
        private System.Windows.Forms.TextBox textPass;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.TextBox textValue;
        private System.Windows.Forms.TextBox textKey;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.Label label3;
    }
}

Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

YAFVision Theme Created by Jaben Cargman (Tiny Gecko)
Powered by Yet Another Forum.net version 1.9.1.8 (NET v2.0) - 3/29/2008
Copyright © 2003-2008 Yet Another Forum.net. All rights reserved.


Sponsored by