StreamWriter not writing to file

public StreamWriter writeFile;

string filename = logDirectory+DateTime.Now.Year+DateTime.Now.Month+"_Ftp.log";

writeFile= new StreamWriter(filename,true);

writeFile.Write("text to write");

This above code does not work for some reason...can anyone tell me why This code is suppose to append some log text each day to the file and a new file is created every month.



Answer this question

StreamWriter not writing to file

  • George2

    You need to close the StreamWriter.

    Ideally you should wrap the output code in a using statement, since StreamWriter implements IDisposable:


    string filename = logDirectory+DateTime.Now.Year+DateTime.Now.Month+"_Ftp.log";
    using(StreamWriter writeFile =
    new StreamWriter(filename,true))
    {
        writeFile.Write("text to write");
    }

     

    That will automatically close the stream and clean up the associated resources.



  • Randy Galliano

    That did it. Thanks!
  • StreamWriter not writing to file