1
var lines2 = File.ReadAllLines("file.path");
File.WriteAllLines("file.path", lines2.Take(lines2.Length - 2).ToArray());
using (StreamWriter db = new FileInfo("file.path").AppendText())
       db.Writeline("");

This is my code to delete the last two lines and write after into a new line my text, this works fine,

but, i need the same to delete the last 2 characters into the last line (text file has only one line) and add to the same line without space or new line, my text.

I tried several times but failed. Also google could not help me.

Example:

text before: Hello, that's an example!!

delete last two characters: Hello, that's an example

add to text: ???

text after: Hello, that's an example???

i want to delete the last two characters !! and add whatever i want behind the last word

bodyXY
  • 29
  • 6

4 Answers4

6

There's no need to read all of the contents if all you want to do is chop off a bit and append. Something like:

using(var fs = new FileStream("file.path"))
{
  fs.Seek(-2,SeekOrigin.End);
  var newText = Encoding.UTF8.GetBytes("new content");
  fs.Write(newText,0,newText.Length);
  fs.SetLength(fs.Position); //Only needed if new content may be smaller than old
}

(If you're not happy with having to do the Encoding step yourself, you can move the SetLength() call to immediately after the Seek, then wrap the FileStream in an appropriate StreamReader and start working with it from there)

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
3

delete the last 2 characters of the last line

You could use this:

string[] allLines = File.ReadAllLines("file.path");
if(allLines.Length > 0)
{
   string lastLine = allLines.Last();
   if(lastLine.Length >= 2)
   {
       lastLine = lastLine.Remove(lastLine.Length - 2);
   }
   allLines[allLines.Length - 1] = lastLine;
}

File.WriteAllLines("file.path", allLines);
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Have tried your code, he writes the text file, but does not delete the last two characters. :( This is only my second program, probably I'm doing something wrong. – bodyXY Oct 05 '18 at 12:38
  • @bodyXY - it's important (for all of us) to know whether the "last line" of the file *includes* line terminators (carriage return and/or line feeds) and whether these should be persisted by this action. You first need to work that out, then *edit* your question to make it clear if such is the case. – Damien_The_Unbeliever Oct 05 '18 at 12:46
  • @bodyXY: you should use the debugger to see what's going wrong. – Tim Schmelter Oct 05 '18 at 12:48
2
// Get file content
string fileContent = File.ReadAllText("file.path");
// Remove the last to characters
fileContent = fileContent.Remove(fileContent.Length - 2) + "your text...";
// Write content back to the file
File.WriteAllText("file.path", fileContent);
martin_jaehn
  • 180
  • 2
  • 11
  • @bodyXY Happy it worked! Remember to mark my solution (or another one, if you think another one is the better solution) as an answer, so people know your problem is solved. – martin_jaehn Oct 05 '18 at 12:48
  • This would be pretty poor performing in relation to some of the other answers, or potential answers... – maccettura Oct 05 '18 at 13:09
  • @bodyXY You should see a checkmark on the left side of answers – martin_jaehn Oct 05 '18 at 13:15
  • @maccettura Probably... OP has to decide. I'm definitely not saying my answer is the best, it's just what works for me and the way I did it up until now. Might consider playing around with Damien's answer though. – martin_jaehn Oct 05 '18 at 13:16
0

How I would do it (I skip steps I assume you know)

First, get the content of the file

Second:

string MyFileContext = MyFileContext.Substring(0, MyFileContext.Length - 2);
MyFileContext += "What you want";

Three: Save it :)

Shmosi
  • 322
  • 2
  • 17