VB - Writing to a file with StreamWriter

被刻印的时光 ゝ 提交于 2019-12-04 04:29:46

问题


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

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 ?


回答1:


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


来源:https://stackoverflow.com/questions/21455612/vb-writing-to-a-file-with-streamwriter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!