我正在尝试使用StreamWriter写入文件。
Dim write as IO.StreamWriter
write = new io.streamwriter(file)
write.write(txtEncryption.text)
write.close
我以调试模式停止了代码,看到它崩溃了,当达到第2行时,它直接呈异常。
这是因为我只是制作了该文件,并且仍在使用?我该如何避免?
Dim write As IO.StreamWriter
Try
write=New IO.StreamWriter(file)
write.write(txtEncryption.text)
Catch ex As Exception
'Prompt error
Console.WriteLine("Error {0}",ex.Message)
Finally
If write IsNot Nothing Then
write.Close()
End If
End Try
假设(如果在其他任何地方都没有打开文件):您已经打开了一个。请确保所有打开的流都正确关闭。您也可以使用此语法
Using writer As StreamWriter = New StreamWriter(file)
writer.Write("....")
//and so on
End Using
Dim FileLic As New System.IO.StreamWriter("Licenseaccepted", False)
'("Licenseaccepted", False)
FileLic.WriteLine(lblLICaccept.Text)
FileLic.WriteLine(AuthorYear)
FileLic.WriteLine("Non cancellare o modificare questo file.")
FileLic.Close()
此解决方案适合我。