vb.net 使用 Visual Basic 将多行写入文本文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14903938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:40:56  来源:igfitidea点击:

Write mutiple lines to a text file using Visual Basic

vb.nettext-filesstreamwriter

提问by Jesse Alford

I'm trying to check to see if a file exists, if so it does nothing. If the file does not exist is creates the text file. Then I want to write text to that file. Where am I going wrong with this code? I'm just trying to write multiple lines to the text file and that part is not working. It is creating the text file... just not writing to it.

我正在尝试检查文件是否存在,如果存在,则不执行任何操作。如果文件不存在,则创建文本文件。然后我想将文本写入该文件。这段代码我哪里出错了?我只是想将多行写入文本文件,但该部分不起作用。它正在创建文本文件......只是不写入它。

Dim file As System.IO.FileStream
 Try
  ' Indicate whether the text file exists
  If My.Computer.FileSystem.FileExists("c:\directory\textfile.txt") Then
    Return
  End If

  ' Try to create the text file with all the info in it
  file = System.IO.File.Create("c:\directory\textfile.txt")

  Dim addInfo As New System.IO.StreamWriter("c:\directory\textfile.txt")

  addInfo.WriteLine("first line of text")
  addInfo.WriteLine("") ' blank line of text
  addInfo.WriteLine("3rd line of some text")
  addInfo.WriteLine("4th line of some text")
  addInfo.WriteLine("5th line of some text")
  addInfo.close()
 End Try

回答by Darin Dimitrov

You don't seem to be properly releasing the resources you allocated with this file.

您似乎没有正确释放您为此文件分配的资源。

Make sure that you always wrap IDisposableresources in Using statements to ensure that all resources are properly released as soon as you have finished working with them:

确保始终将IDisposable资源包装在 Using 语句中,以确保所有资源在使用完后立即正确释放:

' Indicate whether the text file exists
If System.IO.File.exists("c:\directory\textfile.txt") Then
    Return
End If

Using Dim addInfo = File.CreateText("c:\directory\textfile.txt")
    addInfo.WriteLine("first line of text")
    addInfo.WriteLine("") ' blank line of text
    addInfo.WriteLine("3rd line of some text")
    addInfo.WriteLine("4th line of some text")
    addInfo.WriteLine("5th line of some text")
End Using

but in your case using the File.WriteAllLinesmethod seems more appropriate:

但在您的情况下,使用该File.WriteAllLines方法似乎更合适:

' Indicate whether the text file exists
If System.IO.File.exists("c:\directory\textfile.txt") Then
    Return
End If

Dim data As String() = {"first line of text", "", "3rd line of some text", "4th line of some text", "5th line of some text"}
File.WriteAllLines("c:\directory\textfile.txt", data)

回答by John Bustos

It all works great! - This is not the best way to create and write to a file - I'd rather create the text I want to write and then just write it to a new file, but given your code, all that is missing is having to close the created file before writing to it. Just change this line:

这一切都很棒!- 这不是创建和写入文件的最佳方式 - 我宁愿创建我想写入的文本,然后将其写入一个新文件,但鉴于您的代码,所缺少的只是必须关闭在写入文件之前创建文件。只需更改这一行:

file = System.IO.File.Create("c:\directory\textfile.txt")

to:

到:

file = System.IO.File.Create("c:\directory\textfile.txt")
file.close

All the rest will work.

其余的都将起作用。

回答by Anuj Masand

 file = System.IO.File.Create("path")

Close the file once created then try to write to it.

创建后关闭文件,然后尝试写入。

 file.Close()
     Dim addInfo As New System.IO.StreamWriter("path")