text file management
hey, so i'm trying to use the streamwriter function to write in a text file, but it always replace the text it's in it, how do i add lines without deleting the preceding texts?
54 Replies
how are you opening the file?
i'm opening it with the streamwriter function, like that
and i'm doing the same thing when i want to add a new text line
use AppendText to open the file
File.AppendText(String) Method (System.IO)
Creates a StreamWriter that appends UTF-8 encoded text to an existing file, or to a new file if the specified file does not exist.
so
using statement - ensure the correct use of disposable objects - C#...
Use the C# using statement or declaration to ensure the correct use of disposable objects
and it can add a new line without deleting the existing text in the file?
it's called AppendText for a reason 🙂
from the summary on the docs
Creates a StreamWriter that appends UTF-8 encoded text to an existing file, or to a new file if the specified file does not exist.
okay, i'll try that then
uh, now it deletes the second line-
Show your code
this is how i did it
@jcotton42
is that the only place you're writing?
also, how big is the enclosing block?
since the file won't be closed and written until the end of that block
for now there is only two places i use that writing method
the enclosing block is just a bit down, 2 rows
@I can't be used on production
ah let me read
@LostAngel
is this what you wanted?
yeah, and i want it to add more and more textes without having the second text to be overwriten
in the video I sent that doesn't happen
you can keep appending text to the file
you don't need the line in writer.WriteLine ?
the
u8
is the same as doing Encoding.UTF8.GetBytes("This is a test!\n")
That method is not part of a FileStream
that's part of a StreamWriter
yeah, it's part of StreamWriter
Simply use the
File.Open
method with the FileMode.Append
option and you should have no problemlet me test that then ^^'
Did it work?
i'm having a code error when typing the code
(sorry it's in french)
add
u8
at the end of the string
$".."u8
without ,
.
ah right it's interpolated, interpolated strings can't do that
do
Encoding.UTF8.GetBytes("..")
like that?
yeah
i'll test that
it still removes the preceding 2nd text
ok, so basically, if that happens for you (weird asf) I'd say that you could just append two
\n\n
at the end, so it deletes an empty line and adds a next onefile content before :
History of angethemessager :
- 754 : name ★ (ressource)
file content after :
History of angethemessager :
- 755 : name ★ (ressource)
that might be a logic error in another part of the program tho
something that deletes the file somewhere else, because
AppendText
should work and aswell Write
from FileStream
you maybe opening the file in writemode somewhere elseahh
this is the only second time i use the file management there
you are creating the file and reopening it
do this
wait
(it's in another .cs file)
AHH
first of all
name missmatch
Historigue and Historique
oh 🤦
so it recreates the file because Historigue never exists
and also
you could improve efficiency by doing this
well yeah it's that one error that removed all the text each time 🤦
with this you don't call the string interpolation handler two times while checking the file and creating it, and you use the already opened FileStream so you don't close one and open another for no reason
@LostAngel
alrighty
thanks ^^