Example #1
0
        public static string ReadFromCSV(string filename)
        {
            if (false == System.IO.File.Exists(filename))
            {
                return(null);
            }
            Encoding     encoding = Encoding.Default;
            FileStream   fs       = new FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite);
            StreamReader sr       = new StreamReader(fs, encoding);
            string       content  = sr.ReadToEnd();

            sr.Close();
            fs.Close();
            if (null == content)
            {
                return(null);
            }
            string[] arrLine = content.Split('\n');
            if (null == arrLine)
            {
                return(null);
            }

            valueInfoList reta = new valueInfoList();

            //第一行中文备注,第二行key
            string[] arrKey = arrLine[1].Remove(arrLine[1].Length - 1).Split(',');
            for (int i = 2; i < arrLine.Length - 1; i++)
            {
                string strLine = arrLine[i];
                //每行末尾有一个‘/r’
                strLine = strLine.Remove(arrLine[i].Length - 1);
                string[] arrWord = strLine.Split(',');

                valueInfo viNode = new valueInfo();
                for (int j = 0; j < arrKey.Length; j++)
                {
                    viNode.SetValue(arrKey[j], arrWord[j]);
                }
                reta.m_infoList.Add(i.ToString(), viNode);
            }
            return(content);
        }
Example #2
0
        static public valueInfoList ReadFromIni(string filename)
        {
            if (false == System.IO.File.Exists(filename))
            {
                return(null);
            }
            Encoding     encoding = Encoding.Default;
            FileStream   fs       = new FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite);
            StreamReader sr       = new StreamReader(fs, encoding);
            string       content  = sr.ReadToEnd();

            sr.Close();
            fs.Close();
            if (null == content)
            {
                return(null);
            }
            string[] arrLine = content.Split('\n');
            if (null == arrLine)
            {
                return(null);
            }

            valueInfoList reta    = new valueInfoList();
            string        listKey = "";
            valueInfo     viNode  = new valueInfo();

            for (int i = 0; i < arrLine.Length - 1; i++)
            {
                string strLine = arrLine[i];
                //每行末尾有一个‘/r’
                strLine = strLine.Remove(arrLine[i].Length - 1);

                if (string.IsNullOrWhiteSpace(strLine))
                {
                    continue;
                }

                if (strLine.Trim()[0] == '[')
                {
                    if (listKey != "")
                    {
                        reta.m_infoList.Add(listKey, viNode);
                        listKey = "";
                        viNode  = new valueInfo();
                    }
                    listKey = strLine.Substring(1, strLine.Length - 2);
                }
                else
                {
                    string[] arrWord = strLine.Split('=');
                    if (arrWord.Length > 1)
                    {
                        viNode.SetValue(arrWord[0], arrWord[1]);
                    }
                }
            }
            if (listKey != "")
            {
                reta.m_infoList.Add(listKey, viNode);
            }
            else if (viNode.Count > 0)
            {
                reta.m_infoList.Add("CONFIG", viNode);
            }

            return(reta);
        }