Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Sep 19, 2011 07:46 PM by rossmc1
Member
9 Points
23 Posts
Sep 19, 2011 08:19 AM|LINK
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
664 Points
151 Posts
Sep 19, 2011 10:50 AM|LINK
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"); }
644 Points
152 Posts
Sep 19, 2011 07:46 PM|LINK
sw.flush();
sw.close();
sw.dispose();
usually close or dispose is enough.
Lybra
Member
9 Points
23 Posts
StreamWriter & File being used after copy.
Sep 19, 2011 08:19 AM|LINK
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
TP
Member
664 Points
151 Posts
Re: StreamWriter & File being used after copy.
Sep 19, 2011 10:50 AM|LINK
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"); }rossmc1
Member
644 Points
152 Posts
Re: StreamWriter & File being used after copy.
Sep 19, 2011 07:46 PM|LINK
sw.flush();
sw.close();
sw.dispose();
usually close or dispose is enough.