6

This piece of code worked perfectly in VS 2010. Now that I have VS 2013 it no longer writes to the file. It doesn't error or anything. (I get an alert in Notepad++ stating that the file has been updated, but there is nothing written.)

It all looks fine to me. Any Ideas?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            String line;
            try
            {
                //Pass the file path and file name to the StreamReader constructor
                StreamReader sr = new StreamReader("C:\\Temp1\\test1.txt");
                StreamWriter sw = new StreamWriter("C:\\Temp2\\test2.txt");

                //Read the first line of text
                line = sr.ReadLine();

                //Continue to read until you reach end of file
                while (line != null)
                {
                    //write the line to console window
                    Console.WriteLine(line);
                    int myVal = 3;
                    for (int i = 0; i < myVal; i++)
                    {
                        Console.WriteLine(line);
                        sw.WriteLine(line);
                    }
                    //Write to the other file
                    sw.WriteLine(line);
                    //Read the next line
                    line = sr.ReadLine();
                }

                //close the file
                sr.Close();
                Console.ReadLine();
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
            }
            finally
            {
                Console.WriteLine("Executing finally block.");
            }
        }
    }
}
tvoytko
  • 93
  • 1
  • 6
  • are you looking in the right directory? have you opened the file as a sanity check? – user1666620 Jun 17 '15 at 13:21
  • Try explicitly flush as well. – Lloyd Jun 17 '15 at 13:21
  • 4
    I don't see anywhere that you call sw.Close(); If you do that, the write will be flushed and the file closed. Also, you should really look at wrapping the StreamReader and StreamWriter in using blocks - they both implement IDisposable, and will close automatically when you leave the using block – thorkia Jun 17 '15 at 13:23
  • 1
    Additionally, it's a good idea to explicitly close your streams. You're closing your stream reader, but not the writer. – DeadZone Jun 17 '15 at 13:24

2 Answers2

7

You need to close StreamWriter. Like this:

using(var sr = new StreamReader("..."))
using(var sw = new StreamWriter("..."))
{
   ...
}

This will close streams even if exception is thrown.

ranquild
  • 1,799
  • 1
  • 16
  • 25
  • 2
    +1 for best "Best Practices". All IDisposable objects should (almost always) be paired with a using statement. – Matt Murrell Jun 17 '15 at 13:25
  • Yes, using statements should be used - no need to explicitly call Flush/Close, so much neater. – Polyfun Jun 17 '15 at 13:29
  • Also that loop can be cleaned up by combining the readline and the comparison while ((line = sr.ReadLine()) != null) { //write the line to console window Console.WriteLine(line); int myVal = 3; for (int i = 0; i < myVal; i++) { Console.WriteLine(line); sw.WriteLine(line); } //Write to the other file sw.WriteLine(line); } – thorkia Jun 17 '15 at 13:29
5

You need to Flush() the StreamWriter after write.

By default StreamWriter is buffered that means it won't output until it receives a Flush() or Close() call.

Also you can also try to close it like this:

sw.Close();  //or tw.Flush();

You can also have a look at StreamWriter.AutoFlush Property

Gets or sets a value indicating whether the StreamWriter will flush its buffer to the underlying stream after every call to StreamWriter.Write.

The other option which is now a days very popular and recommended is to use the using statement which takes care of it.

Provides a convenient syntax that ensures the correct use of IDisposable objects.

Example:

using(var sr = new StreamReader("C:\\Temp1\\test1.txt"))
using(var sw = new StreamWriter("C:\\Temp2\\test2.txt"))
{
   ...
}
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331