System.IO.File & StreamWriter

Posted on 2005-05-10
Last Modified: 2011-04-14
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?
Question by:addicktz
    LVL 13

    Accepted Solution

    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>
                    Line = <my_data_string>
                    Dim stream_writer As StreamWriter
                    stream_writer = File.AppendText(myFile)
                End If
    LVL 33

    Expert Comment

    Will something like this work?

    Dim sw As StreamWriter = System.IO.File.CreateText("c:\myfile.text")

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
    Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now