讀取設置config.ini配置

 class CSenseIni
    {
        /************************************************************************/
        /*寫操做
         * strSection   節
         * strKey       鍵
         * strValue     須要寫入的值
         * strFilePath  配置文件的全路徑(wince中使用相對路徑)
         */
        /************************************************************************/
        public static void WriteIni(string strSection, string strKey, string strValue, string strFilePath)
        {
            string strCurr = CPlatformConfig.CurrentPath;
            if (strCurr.Length < 2)
                strCurr += strFilePath;
            else
                strCurr += "\\" + strFilePath;
            INICommon(false, strSection, strKey, strValue, strCurr);
        }

        /************************************************************************/
        /* 讀操做
         * strSection   節
         * strKey       鍵
         * strDefault   若是未找到相應鍵對應的值則填入此值
         * strFilePath  配置文件的全路徑(wince中只能使用絕對全路徑)
         * 返回:       指定鍵的相應值
         * 說明:       若是在文件中未找到相應節則添加,未找到相應鍵亦添加,若是鍵對應的值爲空串則使用默認值填充ini文件並返回
        /************************************************************************/
        public static string GetIni(string strSection, string strKey, string strDefault, string strFilePath)
        {
            string strCurr = CPlatformConfig.CurrentPath;
            if (strCurr.Length < 2)
                strCurr += strFilePath;
            else
                strCurr += "\\" + strFilePath;
            return INICommon(true, strSection, strKey, strDefault, strCurr);
        }

        private static string[] Split(string input, string pattern)
        {
            string[] arr = System.Text.RegularExpressions.Regex.Split(input, pattern);
            return arr;
        }
        private static void AppendToFile(string strPath, string strContent)
        {
            if (strContent.Length == 0)
                return;

            FileStream fs = new FileStream(strPath, FileMode.Append);
            StreamWriter streamWriter = new StreamWriter(fs, System.Text.Encoding.Default);
            streamWriter.BaseStream.Seek(0, SeekOrigin.End);
            streamWriter.WriteLine(strContent);
            streamWriter.Flush();
            streamWriter.Close();
            fs.Close();
        }
        private static void WriteArray(string strPath, string[] strContent)
        {
            FileStream fs = new FileStream(strPath, FileMode.Truncate);
            StreamWriter streamWriter = new StreamWriter(fs, System.Text.Encoding.Default);
            streamWriter.BaseStream.Seek(0, SeekOrigin.Begin);

            for (int i = 0; i < strContent.Length; i++)
            {
                if (strContent[i].Trim() == "\r\n")
                    continue;
                if (strContent[i].Trim() == "")
                    continue;
                streamWriter.WriteLine(strContent[i].Trim());
            }

            streamWriter.Flush();
            streamWriter.Close();
            fs.Close();
        }
        //INI解析
        private static string INICommon(bool isRead, string ApplicationName, string KeyName, string Default, string FileName)
        {
            string strSection = "[" + ApplicationName + "]";
            string strBuf;
            try
            {
                //a.文件不存在則建立
                if (!File.Exists(FileName))
                {
                    FileStream sr = File.Create(FileName);
                    sr.Close();
                }
                //讀取INI文件
                System.IO.StreamReader stream = new System.IO.StreamReader(FileName, System.Text.Encoding.Default);
                strBuf = stream.ReadToEnd();
                stream.Close();
            }
            catch (Exception e)
            {

                return Default;
            }

            string[] rows = Split(strBuf, "\r\n");
            string oneRow;
            int i = 0;
            for (; i < rows.Length; i++)
            {
                oneRow = rows[i].Trim();

                //空行
                if (0 == oneRow.Length)
                    continue;

                //此行爲註釋
                if (';' == oneRow[0])
                    continue;

                //沒找到
                if (strSection != oneRow)
                    continue;

                //找到了
                break;
            }

            //b.沒找到對應的section,建立一節並建立屬性
            if (i >= rows.Length)
            {
                AppendToFile(FileName, "\r\n" + strSection + "\r\n" + KeyName + "=" + Default);
                return Default;
            }

            //找到section

            i += 1; //跳過section  

            int bakIdxSection = i;//備份section的下一行

            string[] strLeft;

            //查找屬性
            for (; i < rows.Length; i++)
            {
                oneRow = rows[i].Trim();

                //空行
                if (0 == oneRow.Length)
                    continue;

                //此行爲註釋
                if (';' == oneRow[0])
                    continue;

                //越界
                if ('[' == oneRow[0])
                    break;

                strLeft = Split(oneRow, "=");

                if (strLeft == null || strLeft.Length != 2)
                    continue;

                //找到屬性
                if (strLeft[0].Trim() == KeyName)
                {
                    //
                    if (isRead)
                    {
                        //c.找到屬性但沒有值
                        if (0 == strLeft[1].Trim().Length)
                        {
                            rows[i] = strLeft[0].Trim() + "=" + Default;
                            WriteArray(FileName, rows);
                            return Default;
                        }
                        else
                        {
                            //找到了                        
                            return strLeft[1].Trim();
                        }
                    }

                    //
                    else
                    {
                        rows[i] = strLeft[0].Trim() + "=" + Default;
                        WriteArray(FileName, rows);
                        return Default;
                    }
                }
            }

            //d.沒找到對應的屬性,建立之並賦爲默認
            rows[bakIdxSection] = rows[bakIdxSection] + "\r\n" + KeyName + "=" + Default;
            WriteArray(FileName, rows);
            return Default;
        }
    }
/* 獲取路徑*/
    public class CPlatformConfig
    {
        private static string m_CurrentPath;

        private static string Platform
        {
            get
            {
                return Environment.OSVersion.Platform.ToString();
            }
        }

        //獲取執行文件所在文件夾的絕對位置
        public static string CurrentPath
        {
            get
            {
                //m_CurrentPath = @"D:\Index";
                m_CurrentPath = AppDomain.CurrentDomain.BaseDirectory;
                //if (Platform.Equals("WinCE"))
                //{
                //    m_CurrentPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
                //}
                //else if (Platform.Equals("Win32NT"))
                //{
                //    m_CurrentPath = Directory.GetCurrentDirectory();
                //}

                return m_CurrentPath;
            }
            set
            {
                m_CurrentPath = value;
            }
        }
    }
    
 public class CSenseConfig
    {
        //獲取&設置 語言: 0-中文 1-英文
        public static int GetLanguage()
        {
            string szLang = CSenseIni.GetIni("Language", "IsEnglish", "1", "config.ini");
            return Convert.ToInt32(szLang);
        }

        public static void SetLanguage(int iLanguge)
        {
            CSenseIni.WriteIni("Language", "IsEnglish", iLanguge.ToString(), "config.ini");
        }

    }
相關文章
相關標籤/搜索