-2

I have programmed a C# console application with a StreamWriter: Now I want to close the StreamWriter to write all elements in filename.txt but I want to use the same StreamWriter again, how can I do it? Open after Close?

Example:

StreamWriter Stream = new StreamWriter(filename,true);

for(;;)
{
//this is a endless loop
Stream.WriteLine("THIS MUST BE WRITE TO MY DEVICE NOW")
//now i must use
Stream.Close();
//or
Stream.Dispose();
//or
Stream.Close();

}
// When it begin again from the top of the loop the StreamWriter   doesn´t    work.

Now I want to close the StreamWriter to write all elements in filename.txt but I want to use the same StreamWriter again, how can I do it? Open after Close?

Hesoyam
  • 49
  • 7
  • 1
    Why not just make a new one? – adv12 Dec 21 '16 at 14:18
  • Its a endless loop i wont creat endles new ones, i hope i can make it easy.... – Hesoyam Dec 21 '16 at 14:19
  • 4
    What exactly do you think would be the problem with creating "endless new ones," provided you're properly disposing of the "old ones"? – adv12 Dec 21 '16 at 14:20
  • There's no way of changing the filename of the StreamWriter, so unless you want to keep writing to the same file you'll need a new instance anyway. – stuartd Dec 21 '16 at 14:22
  • I write Stream.Flush(); or Stream.Close(); , or Stream.Dispose(); – Hesoyam Dec 21 '16 at 14:22
  • And than i cant call the same StreamWriter again – Hesoyam Dec 21 '16 at 14:23
  • 2
    `Close` calls `Dispose`, so once you `Close` it, it cannot be reused. https://msdn.microsoft.com/en-us/library/system.io.streamwriter.close(v=vs.110).aspx – wablab Dec 21 '16 at 14:24
  • It's there a another way to Write again with the same StreamWriter? – Hesoyam Dec 21 '16 at 14:26
  • 3
    Why? There's no point. Just create a new one. – Liam Dec 21 '16 at 14:27
  • 4
    Why? There is no good reason to do that. – Igor Dec 21 '16 at 14:27
  • 3
    @Hesoyam, I'm pretty sure you're getting hung up on how to do something you *don't need to do*. What makes you think it will be a problem to create, use, and close several StreamWriters in succession? – adv12 Dec 21 '16 at 14:27
  • 2
    @Hesoyam is not very clear _at least, fo me_ about why you want reuse the `StreamWriter` please edit your question providing more details; maybe there's a better workaround for solve your requirement. – Mauricio Arias Olave Dec 21 '16 at 14:28
  • It can have than more than 100.000 new ones its a endless loop, and I want to write the string into a txt, if I don't make this c# doesn't write it on the physical device... Createing 100.000 new StreamWriters is the best way? than i need one loop more.. – Hesoyam Dec 21 '16 at 14:30
  • 2
    Are you writing to 100000 files, or are you appending to one file 100000 times? If the latter, define your `StreamWriter` outside the loop and don't call `Close()` within the loop; call it when the loop is finished. If you're writing to 100000 files, then yes, create 100000 StreamWriters; just make sure you dispose them immediately when you're done with them (i.e. inside the loop). – adv12 Dec 21 '16 at 14:33
  • 2
    @Hesoyam: Why do you think you need one more loop? Can you edit your question to show your attempted solution *with the loop*, while pointing out what is wrong with it? – O. R. Mapper Dec 21 '16 at 14:34
  • So it seems from your edit that inside your loop you're trying to append to a single file and that you want your changes to be flushed immediately to the file. Correct? Have you tried `Flush()`? That should do it. – adv12 Dec 21 '16 at 14:45
  • Either don't close the writer in the loop; flush it instead. Or put the creation of the writer in the loop. Either way is fine, which you choose is up to you. But once you close a writer, you must never touch it again. – Eric Lippert Dec 21 '16 at 14:48

1 Answers1

1
Stream.Flush();
// it works fine
Termininja
  • 6,620
  • 12
  • 48
  • 49
Hesoyam
  • 49
  • 7
  • 1
    Please don't add "thank you" as an answer. Instead, **[accept the answer](http://stackoverflow.com/help/accepted-answer)** that you found most helpful. - [From Review](/review/low-quality-posts/14663835) – H. Pauwelyn Dec 21 '16 at 17:58
  • @H.Pauwelyn Please read carefully. This is not a 'thank you' answer. – Rob Dec 22 '16 at 03:24