自己寫的 讀寫 ini 設定檔類

來源:互聯網
上載者:User

標籤:

    /// <summary>    /// 不調用系統API 讀寫 ini 設定檔    /// </summary>    public class RW_ini    {        #region ========ini 讀寫========        // 首次調用 RWini 時需要初始化此參數        public static string pathIni;        // 記錄錯誤資訊 與 WriteLog 一起使用        public static string pathErr;        public static string ReadIni(string section, string key)        {            string result = "";            try            {                // 檔案不存在則建立                if (!File.Exists(pathIni))                {                    File.Create(pathIni);                }                else                {                    //讀取INI檔案                    string buffer;                    string[] temp;                    using (StreamReader sr = new StreamReader(pathIni, Encoding.Default))                    {                        while (sr.Peek() >= 0)                        {                            buffer = sr.ReadLine().Trim();                            if (!string.IsNullOrEmpty(buffer) && buffer.StartsWith(string.Format("[{0}]", section), StringComparison.OrdinalIgnoreCase))                            {                                while (sr.Peek() > 0)                                {                                    buffer = sr.ReadLine().Trim();                                    if (!string.IsNullOrEmpty(buffer))                                    {                                        if (buffer.StartsWith("["))                                            break;                                        if (!buffer.StartsWith(";"))                                        {                                            temp = buffer.Split(‘=‘);                                            if (temp.Length > 1 && temp[0].TrimEnd().Equals(key, StringComparison.OrdinalIgnoreCase))                                            {                                                return temp[1].TrimStart();                                            }                                        }                                    }                                }                                // 不再判斷下一個節點                                return result;                            }                        }                    }                }                // 不再判斷下一個節點                return result;            }            catch            {                throw new ApplicationException("同步檔案讀取異常!");            }        }        private void WriteIni(string section, string key, string value)        {            try            {                // 檔案不存在則建立                if (!File.Exists(pathIni))                {                    File.Create(pathIni);                }                // 修改INI檔案                bool modify = false, find = false;                string buffer;                List<string> temp = new List<string>();                // 將所有設定檔放到集合中                using (StreamReader sr = new StreamReader(pathIni, Encoding.Default))                {                    while (sr.Peek() >= 0)                    {                        buffer = sr.ReadLine().Trim();                        temp.Add(buffer);                        if (!modify && !string.IsNullOrEmpty(buffer) && buffer.StartsWith(string.Format("[{0}]", section), StringComparison.OrdinalIgnoreCase))                        {                            find = true;                            while (sr.Peek() >= 0)                            {                                buffer = sr.ReadLine().Trim();                                temp.Add(buffer);                                if (!string.IsNullOrEmpty(buffer))                                {                                    if (buffer.StartsWith("["))                                        break;                                    if (!modify && !buffer.StartsWith(";") && buffer.Split(‘=‘)[0].TrimEnd() == key)                                    {                                        modify = true;                                        temp.RemoveAt(temp.Count - 1);                                        if (value != null)                                        {                                            temp.Add(string.Format("{0} = {1}", key, value));                                        }                                    }                                }                            }                            if (!modify)                            {                                temp.Insert(temp.Count - 1, string.Format("{0} = {1}", key, value));                            }                        }                    }                    if (!modify && !find)                    {                        temp.Add(string.Format("[{0}]", section));                        temp.Add(string.Format("{0} = {1}", key, value));                    }                }                using (StreamWriter sw = new StreamWriter(pathIni, false, Encoding.Default))                {                    foreach (string item in temp)                        sw.WriteLine(item);                }            }            catch            {                throw new ApplicationException("同步檔案寫入異常!");            }        }        public static void WriteLog(string content)        {            File.AppendText(pathErr).WriteLine("時間:{0} 資訊:{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm"), content);        }    }

因為 WinCE 不帶讀寫 ini 的API 而且自己用的設定檔節點並不多

因此自己編寫此類 用於 WinCE 系統中。C# 代碼

自己寫的 讀寫 ini 設定檔類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.