![]() |
HELP: Problem creating/editing file
I want to write to a simple text file. If it doesn't exist I want to create
it first. Here is my code (portion): Imports System.IO ' at the top of the class : : Dim strFileName As String = "C:\MyFile.txt" ' create file if not exist If Not File.Exists(strfilename) Then File.CreateText(strFileName) Dim swStreamWriter As New StreamWriter(strFileName, True) With swStreamWriter .WriteLine("This is a test. Please append/write to the file!!!!!") .Close() End With The file creates fine if it doesn't exist. The problem is that when it gets to the Dim for the StreamWriter it gives me this type of error: The process cannot access the file "C:\MyFile.txt" because it is being used by another process. The source is mscorlib. Also, when I stop the debug I can't delete the file because it says that a "sharing violation" exists and "The source or destination file may be in use." What am I doing wrong? What is the best way to do this? |
Re: Problem creating/editing file
Hi VB programmer,
You want to try this? Cor \\\ > Imports System.IO ' at the top of the class > : > : > Dim strFileName As String = "C:\MyFile.txt" Dim swStreamWriter As New StreamWriter > ' create file if not exist > If Not File.Exists(strFileName) Then swStreamWriter = File.CreateText(strFileName) swStreamWriter.flush swStreamWriter.close swStreamWriter = File.AppendText(strFileName) > swStreamWriter.WriteLine("This is a test.") swStreamWriter.Flush swStreamWriter.Close() /// Cor |
Re: Problem creating/editing file
Thanks that worked!
"Cor" <non@non.com> wrote in message news:uroNF7CtDHA.2304@tk2msftngp13.phx.gbl... > Hi VB programmer, > > You want to try this? > > Cor > \\\ > > Imports System.IO ' at the top of the class > > : > > : > > Dim strFileName As String = "C:\MyFile.txt" > Dim swStreamWriter As New StreamWriter > > ' create file if not exist > > If Not File.Exists(strFileName) Then > swStreamWriter = File.CreateText(strFileName) > swStreamWriter.flush > swStreamWriter.close > swStreamWriter = File.AppendText(strFileName) > > > swStreamWriter.WriteLine("This is a test.") > swStreamWriter.Flush > swStreamWriter.Close() > /// > Cor > > |
Re: HELP: Problem creating/editing file
It seems like sometimes the file does not get closed properly.
Try putting your code in a Try/Catch/Finally block. In the Finally block, close the stream if the stream object still exist. For example, finally { if (swStreamWriter != null) { swStreamWriter.close(); } } Tommy, "VB Programmer" <growNO-SPAM@go-intech.com> wrote in message news:<OJCZNiCtDHA.2208@TK2MSFTNGP10.phx.gbl>... > I want to write to a simple text file. If it doesn't exist I want to create > it first. Here is my code (portion): > Imports System.IO ' at the top of the class > : > : > Dim strFileName As String = "C:\MyFile.txt" > > ' create file if not exist > If Not File.Exists(strfilename) Then > File.CreateText(strFileName) > > Dim swStreamWriter As New StreamWriter(strFileName, True) > > With swStreamWriter > .WriteLine("This is a test. Please append/write to the > file!!!!!") > .Close() > End With > > The file creates fine if it doesn't exist. The problem is that when it gets > to the Dim for the StreamWriter it gives me this type of error: > The process cannot access the file "C:\MyFile.txt" because it is being used > by another process. > The source is mscorlib. > > Also, when I stop the debug I can't delete the file because it says that a > "sharing violation" exists and "The source or destination file may be in > use." > > What am I doing wrong? What is the best way to do this? |
Re: Problem creating/editing file
Hello, Cor:
I think the problem was that the line "If Not File.Exists(strfilename) Then File.CreateText(strFileName)" created a StreamWriter object that is never closed (in the code) so the file is in use until the GC closes it, some time after the procedure ends, don't you think so? In that case, the swStreamWriter.flush calls shouldn't be necessary. Am I right? Regards. |
Re: Problem creating/editing file
Hi Jose,
No there where at least two errors, the close and the open from the file, that was without an append. About that flush I think that you are right but because that error with the close of the full disk last time I added it, dont know why, something tells me now that it is stupid thinking so, and that where not the errors. Cor >I think the problem was that the line "If Not File.Exists(strfilename) Then >File.CreateText(strFileName)" created a StreamWriter object that is never >closed (in the code) so the file is in use until the GC closes it, some time >after the procedure ends, don't you think so? >In that case, the swStreamWriter.flush calls shouldn't be necessary. Am I >right? |
| All times are GMT. The time now is 05:18 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.