locked
StreamWriter & File being used after copy. RRS feed

  • Question

  • User-135085019 posted

    Hello everybody,

    I have a sub that copies and renames a file:

    My.Computer.FileSystem.CopyFile(sourcefile, destfile)

    And since here everything is working fine, the file is copied succesfully!!!

    The problem comes out when, after, in another sub, I try to write something inside the file copied because for vb the file is still being used!!!

    So if I call:

            sw = New StreamWriter(destfile)

    I receive the error.

    I can't kill the process because the process that's keeping the file in use is my app; what the simplest way to "close" the file?

    Many thanks in advance for your help.

    Regards,
    Lybra

    Monday, September 19, 2011 4:19 AM

Answers

  • User567269486 posted

    you should be using this to dispose the object implicitly.

    using (StreamWriter writer = new StreamWriter("important.txt"))
    	{
    	    writer.Write("Word ");
    	    writer.WriteLine("word 2");
    	    writer.WriteLine("Line");
    	}
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 19, 2011 6:50 AM

All replies

  • User567269486 posted

    you should be using this to dispose the object implicitly.

    using (StreamWriter writer = new StreamWriter("important.txt"))
    	{
    	    writer.Write("Word ");
    	    writer.WriteLine("word 2");
    	    writer.WriteLine("Line");
    	}
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 19, 2011 6:50 AM
  • User1079455108 posted

    sw.flush();

    sw.close();

    sw.dispose();

    usually close or dispose is enough.

    Monday, September 19, 2011 3:46 PM