-2

How do I use the Stream object to write text to a file? It is asking for bytes...

Here is the code:

public override void UpdateLog(string emailId)
    {
        using(Stream s = File.OpenWrite(logFile))
            s.Write(emailId);
    }

emailId should be written as a line of text in the file. Also, do I need to include \n to get the text to write as a new line?

Chris
  • 28,822
  • 27
  • 83
  • 158
  • 3
    A quick [google](https://msdn.microsoft.com/en-us/library/6ka1wd3w%28v=vs.110%29.aspx) search will do the job... – Eminem May 20 '15 at 16:35
  • Look into the `StreamWriter` class – Kevin DiTraglia May 20 '15 at 16:36
  • 1
    Or better yet, don't use streams at all if there's no compelling need to stream the data (as there isn't here) and just use the more appropriate `File` methods that do *exactly* what you want. – Servy May 20 '15 at 16:37
  • Yeah, that directed me to use streamwriter...which worked...but it rewrote the file instead of appending when called. – Chris May 20 '15 at 16:37
  • You will find a way to easily add a line in a txt file in [this SO post][1] [1]: http://stackoverflow.com/questions/8255533/how-to-add-new-line-into-txt-file – PMerlet May 20 '15 at 16:39
  • Ah, so add the second parameter to it. Trivial, but difficult to find. Thank you. – Chris May 20 '15 at 16:42
  • @bordeo It's difficult to find *when you don't look*. If you had actually looked at the documentation of the type it would have been readily apparent, or searched for that information using a search engine. – Servy May 20 '15 at 16:44
  • google File append text and you'll get great MSDN articles – D. Ben Knoble May 20 '15 at 16:47
  • Thanks man, just used the File method in my code. Coming from Linux where you just pipe your stuff into files, its a little tricky. – Chris May 20 '15 at 16:50
  • C# is not equivalent of shell scripts, PowerShell (or even just CMD) is close... Obviously you can pipe output to file in Windows too, but to support that you need to read/write to standard input.output (aka Console.Write/Read) - same way as in all *nix systems. Not sure why you think it is trickier. – Alexei Levenkov May 20 '15 at 17:02
  • Side note: I think your post is not asking about what you want to achieve, but rather what you think you want to do. These are potentially unrelated things... – Alexei Levenkov May 20 '15 at 17:04

3 Answers3

1

You should use the StreamWriter class like this instead to append:

public override void UpdateLog(string emailId)
{
    using (StreamWriter writer = File.AppendText(path))
    {
        writer.writeLine(emailId);
    }
}

Path should be a string path to the file.

D. Ben Knoble
  • 4,273
  • 1
  • 20
  • 38
0

You should use the StreamWriter class. And do something similar to the following:

using (StreamWriter writer =
        new StreamWriter("somefile.txt", true))
    {
        writer.Write("hello");
        writer.WriteLine("hello again");
        writer.WriteLine("and again");
    }
William Xifaras
  • 5,212
  • 2
  • 19
  • 21
0

If you want to keep your current code you can extend it like this:

public override void UpdateLog(string emailId)
{
    using (Stream s = File.OpenWrite(logFile))
    {
        using (StreamWriter writer = new StreamWriter(s))
        {
            writer.Write(emailId);
        }
    }
}
Robert S.
  • 1,942
  • 16
  • 22