HELP: Problem creating/editing file

V

VB Programmer

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?
 
C

Cor

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
 
V

VB Programmer

Thanks that worked!

Cor said:
Hi VB programmer,

You want to try this?

Cor
\\\
Dim swStreamWriter As New StreamWriter
swStreamWriter = File.CreateText(strFileName)
swStreamWriter.flush
swStreamWriter.close
swStreamWriter = File.AppendText(strFileName)
swStreamWriter.WriteLine("This is a test.")
swStreamWriter.Flush
swStreamWriter.Close()
///
Cor
 
T

Tommy

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,
 
?

=?Windows-1252?Q?Jos=E9_Manuel_Ag=FCero?=

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.
 
C

Cor

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top