-2

What I need are:-

  1. delete a line from file with line break.
  2. copy the content to another file.

Assume the file content is as the following:-

sometextsometextsometext
sometextsometextsometext
sometextsometextsometext
sometextsometextsometext
111111111
sometextsometextsometext
sometextsometextsometext

I wanna delete the line that contains 111111111 so I used the next code:-

string content = File.ReadAllText(sOutputPathFileFrom);
content = content.Replace("111111111", "");
File.AppendAllText(sOutputPathFileTo, content);

The result of sOutputPathFileTo is as following:-

sometextsometextsometext
sometextsometextsometext
sometextsometextsometext
sometextsometextsometext

sometextsometextsometext
sometextsometextsometext

The desired result is as next:-

sometextsometextsometext
sometextsometextsometext
sometextsometextsometext
sometextsometextsometext
sometextsometextsometext
sometextsometextsometext

Note:- sOutputPathFileFrom & sOutputPathFileFrom are variables carries the paths of from and to files.

ahmed abdelqader
  • 3,409
  • 17
  • 36
  • sounds like a good plan.. now I would suggest you start with coding something on your own.. this is not a code factory site.. not to mention it's not that difficult of a task to accomplish.. good luck – MethodMan Feb 03 '17 at 19:01
  • replace "" with Eviroment.NewLine or \r\n – bto.rdz Feb 03 '17 at 19:01
  • 2
    @MethodMan: The OP has coded something, its in the question. While I would agree that it is not hard that might be because I have a lot of experience with c#. If you've never had to deal with new lines before you may well be confused about how to get it working. – Chris Feb 03 '17 at 19:03

2 Answers2

1

change code:

content = content.Replace("111111111"+Eviroment.NewLine, "");
Reza ArabQaeni
  • 4,848
  • 27
  • 46
0

I would use a regex, because you may have spaces between the end of the line and the line brake that you are not seeing. This will replace the string, and all the spaces between the last 1 and the new line.

Regex rgx = new Regex(@"111111111\s*\n");
content = rgx.Replace(content, "");

Hope it helps.

AxelWass
  • 1,321
  • 12
  • 21