抱歉标题不好但我不知道如何描述我的问题。
当我运行应用程序时。它没有将文本写入文件。我添加了MessageBox,以查看代码中断的位置。显示了MsgBox 1和2,但是3不是。因此代码在以下情况后中断:
file = My.Computer.FileSystem.OpenTextFileWriter("C:\text.txt", True)
为什么?
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
答案 0 :(得分:0)
我建议使用try..catch statetement ..not messagebox来获取你的错误,它会抓住你的问题;例如:如果文件不存在;
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