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