VB-使用StreamWriter写入文件 [英] VB - Writing to a file with StreamWriter
本文介绍了VB-使用StreamWriter写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用StreamWriter写入文件。
I'm trying to write to a file using the StreamWriter.
Dim write as IO.StreamWriter
write = new io.streamwriter(file)
write.write(txtEncryption.text)
write.close
我在调试模式下停止了代码,我看到它崩溃并在到达第2行时直接转到异常。
I stopped the code in debug mode and i saw it crashes and goes straight to the exception when reaches line 2.
这是因为我只是制作了文件,并且文件仍在使用中?我该如何避免呢?
It is because i just made the file and it's still in use ? How can i avoid that ?
推荐答案
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
假设(如果未在其他任何地方打开文件):打开已打开的文件。确保所有打开的流均正确关闭。您也可以使用这种语法
Assumption (if file was not opened anywhere else) : You open already opened one.Make sure that all your opened streams closed properly. You can use this syntax too
Using writer As StreamWriter = New StreamWriter(file)
writer.Write("....")
//and so on
End Using
这篇关于VB-使用StreamWriter写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文