我正在尝试使用 StreamWriter 写入文件。

Dim write as IO.StreamWriter
write = new io.streamwriter(file)
write.write(txtEncryption.text)
write.close

我在 Debug模式下停止了代码,我看到它崩溃并在到达第 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

关于vb.net - VB - 使用 StreamWriter 写入文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21455612/

10-13 09:00
查看更多