• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2067
  • Last Modified:

System.IO.File & StreamWriter

Ok, I am looking to delete an existing file, and then open a stream writer to append text to the file over the duration of the programs run.

I am using File.Delete in my sub new to delete the file
then with a public delcared streamwriter
i am using

streamw = file.appendtext(path)

but it tells me the file does not exist, along with File.CreateText, when I use File.Create(path) before the streamw, the streamw line errors telling me the file is already in use, any ideas?
0
addicktz
Asked:
addicktz
1 Solution
 
TorrwinCommented:
Here is how I handle a file not existing, and/or appending to the file:

            Dim storefile As Directory
            Dim Line As String
            Dim myFile As String = (("C:\SimilaritiesLog_Test.txt"))

            If Not File.Exists(myFile) Then
                Dim stream_writer As New IO.StreamWriter(myFile)  <--- Creates the file
                Line = <my_data_string>
                stream_writer.WriteLine(Line)
                stream_writer.Close()
            Else
                Line = <my_data_string>
                Dim stream_writer As StreamWriter
                stream_writer = File.AppendText(myFile)
                stream_writer.WriteLine(Line)
                stream_writer.Close()
            End If
0
 
raterusCommented:
Will something like this work?

File.Delete("c:\somefile.txt")
Dim sw As StreamWriter = System.IO.File.CreateText("c:\myfile.text")
sw.Write("blah")
sw.Close()
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now