C#读取txt文档

废话不多说,直接上源码(控制台应用程序写的)

 public static void ReadTXTFile()
        {
            try
            {
                FileInfo handlingFile = null;
                //指定文档路径
                DirectoryInfo sourceFolder = new DirectoryInfo(@"D:\netnetnetnetnetnetnet\ConsoleApplication1\ConsoleApplication1\bin\Debug");
                FileInfo[] txtFiles = sourceFolder.GetFiles("*.txt") ;//指定文档格式

                for (int idxFile = 0; txtFiles != null && idxFile < txtFiles.Length; ++idxFile)//遍历每个txt文件
                {
                    List<string> lstAddStr = new List<string>();
                    handlingFile = txtFiles[idxFile];

                    StreamReader srTxt = new StreamReader(handlingFile.OpenRead(), System.Text.Encoding.Default);

                    string strReadLine = srTxt.ReadLine();
                    for (; strReadLine != null; strReadLine = srTxt.ReadLine())
                    {
                        if (strReadLine.Trim() == string.Empty)//去掉空白行
                        {
                            continue;
                        }
                        lstAddStr.Add(strReadLine);//读取文档每一行 add 到lstAddStr
                    }

                    Dictionary<string, string> dictPara = new Dictionary<string, string>();

                    for (int i = 0; i < lstAddStr.Count; i++)//遍历每个行
                    {
                        Console.WriteLine(lstAddStr[i]);

                        //if (lstAddStr[i].StartsWith("1")) //对数据进行判断处理
                        //{
                        //   
                        //}
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

 

运行结果:

 

posted @ 2019-01-07 17:19  xmy_007  阅读(2136)  评论(0编辑  收藏  举报