天天看點

用C#實作Delphi的TStringList類 用C#實作Delphi的TStringList類

Delphi的TStringList實在太好用啦,不但可以讀取/寫入文本檔案,而且還可以像Value["KeyName"]這樣使用,剛學C#沒多久,教程上沒有類似的類,自己學習寫一下吧。代碼如下:

using System;

using System.Text;

namespace OverBlueUtil.StringUtils

{

    /// 

    /// 字元串清單對象,可以儲存多行的字元串,可以儲存字元串清單到檔案中,也可以從文本檔案中讀取

    /// 資料到字元串清單中。

    /// 

    class StringList

    {

        private System.Collections.ArrayList Lines = new System.Collections.ArrayList();

        /// 

        /// 建立一個字元串清單對象

        /// 

        public StringList()

        {

        }

        /// 

        /// 直接傳回字元串清單的第 Index 行字元串

        /// 

        ///

指定傳回哪行

        /// 傳回指定行的資料

        public string this[int Index]

        {

            get 

            {

                if (Lines.Count <= Index)

                {

                    throw new InvalidOperationException("Index值超界啦 !");

                }

                else

                {

                    return Lines[Index].ToString();

                }

            }

            set 

            {

                if (Lines.Count <= Index)

                {

                    throw new InvalidOperationException("Index值超界啦 !");

                }

                else

                {

                    Lines[Index] = value;

                }

            }

        }

        /// 

        /// 傳回此字元串清單總行數

        /// 

        /// 

        public int Count()

        {

            return Lines.Count;

        }

        /// 

        /// 增加一行字元串到字元串清單中

        /// 

        /// 

        public int Add(string StrValue)

        {

            return Lines.Add(StrValue);

        }

        /// 

        /// 清除字元串清單中的所有内容

        /// 

        public void Clear()

        {

            Lines.Clear();

        }

        /// 

        /// 讀取字元串清單中 KeyName = ??? 的值        

        /// 

        ///

要讀取的鍵值

        /// 指定KeyName的值,如果字元串清單中不包含指定的KeyName,則傳回空字元串

        public string GetValue(string KeyName)

        {

            if (Lines.Count == 0) return "";

            for (int i = 0; i < Lines.Count; i++)

            {

                string tmpKeyName = Lines[i].ToString().Substring(0, KeyName.Length + 1).ToLower();

                if (KeyName.ToLower() + "=" == tmpKeyName)

                {

                    string tmpValue = Lines[i].ToString();

                    tmpValue = tmpValue.Substring(KeyName.Length + 1, tmpValue.Length - KeyName.Length - 1);

                    return tmpValue;

                }

            }

            return "";                            

        }

        /// 

        /// 讀入字元串清單中 KeyName = ??? 的值,如果指定的KeyName不存在,則自動建立

        /// 

        ///

要寫入的鍵值

        public void SetValue(string KeyName, string KeyValue)

        {

            int Index = -1;

            //查找 KeyName 在第幾行

            if (Lines.Count != 0)

            {

                for (int i = 0; i < Lines.Count; i++)

                {

                    string tmpKeyName = Lines[i].ToString().Substring(0, KeyName.Length + 1).ToLower();

                    if (KeyName.ToLower() + "=" == tmpKeyName)

                    {

                        Index = i;

                        break;

                    }

                }

            }

            if (Index == -1)

            {

                //如果沒有鍵則添加一個

                Lines.Add(KeyName + "=" + KeyValue);

            }

            else

            {

                //修改此鍵的值

                Lines[Index] = KeyName + "=" + KeyValue;

            }

        }

        /// 

        /// 儲存字元串清單到指定的文本檔案中

        /// 

        ///

文本檔案名

        public void SaveToFile(string FileName)

        {                                    

            System.IO.File.WriteAllLines(FileName, (string[])Lines.ToArray(typeof(string)));

        }

        /// 

        /// 從指定的文本中讀取文本内容到字元串清單中

        /// 

        ///

要讀取文本檔案名

        /// 成功則傳回 True,否則傳回 False

        public bool LoadFromFile(string FileName)

        {

            if (System.IO.File.Exists(FileName) == true)

            {

                Lines.Clear();

                Lines.AddRange(System.IO.File.ReadAllLines(FileName));

                return true;

            }

            else

            {

                throw new InvalidOperationException(FileName+" 檔案不存在 !");

                return false;

            }

        }

    }

}測試一下:

1)簡單寫一個字元

StringList sl = new StringList();

sl.Add("這是一個測試");

sl.SaveToFile("C:\\ABC.TXT");2)寫入并讀取Value值

StringList sl = new StringList();

sl.SetValue("ConnStr", "C:\ABCDEFG.DBF");

sl.SaveToFile("C:\\ABC.TXT");  //看看C:\ABC.TXT的内容是不是ConnStr=C:\ABCDEFG.DBF??sl.SetValue("ConnStr", "C:\HIJKLMN.DBF");

sl.SaveToFile("C:\\ABC.TXT");  //看看C:\ABC.TXT的内容是不是ConnStr=C:\HIJKLMN.DBF??

sl.Clear();

sl.LoadFromFile("C:\\ABC.TXT");

MessageBox.Show(sl.GetValue("ConnStr"));  //應該顯示C:\HIJKLMN.DBF

MessageBox.Show(sl.GetValue("CCCCC"));  //應該顯示"",因為根本沒有此KeyName

用C#實作Delphi的TStringList類