C#读取txt作为数据源

 //读取txt文件
        public void ReadTxt()
        {
            string path = @"C:\Users\Administrator\Desktop\1.txt";//文件路径
            string readline;
            string[] array;

            StreamReader reader = new StreamReader(path, System.Text.Encoding.GetEncoding("GB2312"));
            while (reader.Peek()>=0)
            {
                try
                {
                    readline = reader.ReadLine();
                    if (readline != "")
                    {
                        readline = readline.Replace("\"", "");
                        array = readline.Split(',');
                        if (array.Length == 0)
                        {
                            Console.WriteLine("导入的数据有错误");
                            return;
                        }

                        this.DropDownList1.Items.Add(array[0]);

                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }
        }

 

posted @ 2015-07-10 17:04  成神之路~  阅读(273)  评论(0)    收藏  举报