Stream Reader Readline detect newline characters

后端 未结 3 580
无人共我
无人共我 2021-01-16 17:19

I\'m trying to remove any \"new line\" characters from each line of text in my log file.

Below is an example entry that I am reading in with a Stream Reader :-

3条回答
  •  猫巷女王i
    2021-01-16 17:42

    i dont know if anyone else was having exactly this issue. here is the code i used to fix this issue.

          using (System.IO.FileStream File = new System.IO.FileStream(e.FullPath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite))
                {
                    using (System.IO.StreamReader Reader = new System.IO.StreamReader(File, Encoding.Default))
                    {
                        String CompleteData = Reader.ReadToEnd();
                        foreach (String Line in CompleteData.Split(new char[] { (char)13 }, StringSplitOptions.RemoveEmptyEntries))
                        {
                            if (Line.Split(new String[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)[0].Contains("Raising event"))
                            {
                                 //Do Stuff
                            }
                        }
                        Reader.Close();
                    }
                    File.Close();
                }
    

    For some reason i had to do this because just using streamreader would throw an exception saying that the file is in use from another process.

    It might help someone else at a later date..

提交回复
热议问题