0

I did some google searching and everyone gives me the same answer which isn't working. I hope its something simple I am missing. I am trying to test write a line to a txt file. The file was created just fine when I used similar code, and no errors are thrown, the txt just doesn't write/save to the file. I am using stream writer in VB. Here is my code:

   Imports System.IO
Public Class Form1
Private Sub btnGen2DArray_Click(sender As Object, e As EventArgs) 
Handles btnGen2DArray.Click
    Try

        'this is the file created and where it is saved:
        Dim fileLoc As String = "c:\Users\clint\save.txt"
        If File.Exists(fileLoc) Then
            Using sw As New StreamWriter(fileLoc)
                sw.Write("Test line write")
                sw.Close()
            End Using
        End If
        MsgBox("C++ 2D array text file created in: " + fileLoc, MsgBoxStyle.OkOnly, "Successful")
    Catch ex As Exception
        MsgBox("error: " + e.ToString(), MsgBoxStyle.OkOnly, "Error")
    End Try
End Sub
Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
    Close()
End Sub
End Class

I am using vb 2012 if this helps. Normal windows form application.

Clint L
  • 1,093
  • 5
  • 12
  • 29
  • The solution is simple - don't use VS 2003. :) – Victor Zakharov Apr 21 '14 at 15:02
  • Am now using VS 2012, same issue. :) – Clint L Apr 21 '14 at 15:38
  • Do you get an error? Do you get the "C++ 2D array ..." pop-up? – Daniel Apr 21 '14 at 15:42
  • Still no error, the msgbox pops up correctly. – Clint L Apr 21 '14 at 15:43
  • Now that's weird! Usually upgrading to VS 2012 solves all sorts of problem. :) As a test, remove all irrelevant code, and just ALWAYS write some "test line" into the file, see if it works. It helps to reduce scope of the problem. – Victor Zakharov Apr 21 '14 at 15:45
  • Ok, removed irrelevant code not yet used. Still nothing, put a test line in manually and ran the code. The result is the same successful message, and the original line is still there, no additions/changes to the file. Am I needing to open the file before writing or something simple like that? All the examples I see on the web are pretty much the same as mine. Will update question with "bare-bone" code. – Clint L Apr 21 '14 at 15:48

1 Answers1

3

You need to close your StreamWriter when you are done. sw.Close

You must call Close to ensure that all data is correctly written out to the underlying stream.

Better yet, use Using. The following would go inside your if:

Using sw As New StreamWriter(fileLoc)
    sw.Write("Test line write")
        For rowcount As Double = 1 To rows
            For colcount As Double = 1 To cols
                'when the file write test works I will finish the rest of the code here
            Next
        Next
End Using

This will automatically dispose of the StreamWriter for you.

Daniel
  • 12,982
  • 3
  • 36
  • 60
  • Hey thanks Daniel for the quick reply, I tried the close method you first pointed out, and while there are still no errors, the file is still blank. I then tried the using method and got a "using not declared" error. I did some googling and everything says i need to use imports system.configuration, which I did and it still throws the error. Any other thoughts? – Clint L Apr 21 '14 at 14:53
  • 1
    Right... Using requires > Visual Studio 2005 why are you using such old software? – Daniel Apr 21 '14 at 15:00
  • That is the software I have currently, I have to believe there is a way to do this with this limitation. If I can't get this I might switch programming languages as the end result is all that matters to me. Though I would like to figure this out for the sake of figuring it out. – Clint L Apr 21 '14 at 15:20
  • I presume you are using .NET Framework 1.1? As I do not have that Framework installed, perhaps this will be helpful: http://msdn.microsoft.com/en-us/library/6ka1wd3w(v=vs.71).aspx Or possibly this http://stackoverflow.com/q/15443739/1316573 or this http://msdn.microsoft.com/en-us/library/aa287548(v=vs.71).aspx – Daniel Apr 21 '14 at 15:25
  • Ok, upgraded to VS2012 (much better btw) edited changes into the question. Still have same problem using the "Using" block you requested, the file is still blank. – Clint L Apr 21 '14 at 15:36
  • Hey Daniel, I got it to work with the Using statement after changing the \ to / in the path name. Should that have mattered? Thanks for the help. – Clint L Apr 21 '14 at 16:03