过去几天我一直在创建一个程序。程序存储一个字符串“Dim info As String = Nothing”,稍后会询问输入。
EG:我输入“Hello World!”对于变量'info',如何将'info'(Hello World!)的值存储到.txt文件中?或者这不可能吗?
(仍然是vb.net的初学者,寻找简单的答案。)
尝试1(请查找错误):
Sub Main()
Dim path As String = "C:\VBTextFiles\Test1.txt"
Dim stringex As String = "Hello world!"
File.WriteAllText(path, stringex)
End Sub
答案 0 :(得分:2)
您可以使用IO.File.WriteAllText创建文件并将info
写入其中。
This page包含创建和附加的示例。以下是创建的重要部分:
Dim path As String = "c:\temp\MyTest.txt"
' This text is added only once to the file.
If File.Exists(path) = False Then
' Create a file to write to.
Dim createText As String = "Hello and Welcome" + Environment.NewLine
File.WriteAllText(path, createText)
End If
对于APPEND,请使用AppendAllText。请参阅Example。
答案 1 :(得分:-1)
Dim path As String = "c:\temp\MyTest.txt"
' This text is added only once to the file.
If File.Exists(path) = True Then
' Create a file to write to.
Dim createText As String = "Hello and Welcome" + Environment.NewLine
File.WriteAllText(path, createText)
End If
在第3行中将True更改为True:)