0

Sorry for the bad title but i don't know how to describe my problem in short.

When i run the application. It didn't write the Text to the file. I have added the MessageBox's to see where the code interrupts. MsgBox 1 and 2 are shown, but 3 isn't. So the code interrupts after:

file = My.Computer.FileSystem.OpenTextFileWriter("C:\text.txt", True)

Why is that?

Form1.vb:

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim main As New Main()
        Call main.Main()
    End Sub
End Class

Main.vb:

Public Class Main
Sub Main()
    MessageBox.Show("1")
    Dim file As System.IO.StreamWriter
    MessageBox.Show("2")
    file = My.Computer.FileSystem.OpenTextFileWriter("C:\text.txt", True)
    MessageBox.Show("3")
    file.WriteLine("Text")
    file.Close()
End Sub
Craxxurz
  • 105
  • 8

1 Answers1

0

I would recommend to use a try..catch statetement ..not messagebox to get your error, it will catch your problem with cause; eg: if file does not exist;

Public Class Main
    Sub Main()
        Dim file As System.IO.StreamWriter = My.Computer.FileSystem.OpenTextFileWriter("C:\text.txt", True)
        Try

            file.WriteLine("Text")

        Catch ex As Exception

            MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Finally
            File.Close()

        End Try
    End Sub

End Class
  • Oh, i thought that the file will be automatically created. How i can create the file? – Craxxurz Apr 12 '16 at 14:00
  • Comment out your code for the Class, Copy Code as above into your app, revuild and run... read a little on Try Catch and Finally, will save you a lot of headaches in the future – MarkAnderson2001 Apr 12 '16 at 14:04