VB.Net (2019) 文件 I/O 混合数据 请帮助?

VB.Net (2019) File I/O Mixed Data Help Please?

我老了,习惯了 Excel 中较旧的 VBA 和较旧的 VB 代码。现在我正尝试在 Visual Studio 社区与 VB 一起工作。我曾寻求帮助,但我为 .NET 找到的所有内容都带有我不遵循的 C++ 或 C# 代码。我什至找不到一本书来帮助转换为更新的代码格式。我习惯了;

Dim outpath as integer
Dim aString as String
Dim aDouble as Double
aString = "Some Text"
aDouble = 3.1429
outpath = FreeFile
Open "C:\afolder\somedata.dat" For Output As outpath
print #outpath, aString
print #outpath, aDouble
close #outpath

要读取数据,我习惯使用 aString = input #outpath 指令。 从我读到的内容来看,我似乎应该使用 IO.Stream 函数,但没有找到任何可以复制我在上面习惯的旧方法的方法。

谁能给我指点一些在 VB 中涵盖此内容的网页,而不是陷入我无法翻译成 VB 的 C 代码。

谢谢你。

当然,有多种方法可以做到这一点。我也是从 VB6 过来的,还有很多 VBA.

那么,要转换以上内容并写入文件?

好吧,一个方法(我们甚至在 VBA 中做到了这一点)是创建一个字符串,将值集中到字符串中,然后将字符串写入文件。

这很容易,但你必须在代码中折腾下一行字符。

所以,这会起作用:

   Dim aString As String
    Dim aDouble As Double

    aString = "Some Text"
    aDouble = 3.1429

    Dim strPath As String = "C:\test\somedata.dat"

    Dim strOutBuf As String = ""
    strOutBuf = aString
    strOutBuf = strOutBuf & vbCrLf & aDouble

    File.WriteAllText(strPath, strOutBuf)

因此,对于几行简单的文本 - 非常简单。

但是,对于很多代码,逐行输出——类似于VBA?

然后你这样做:

    Dim FILE_NAME As String = "C:\test\test2.txt"
    Dim MyFileWriter As New StreamWriter(FILE_NAME)

    Dim aString As String
    Dim aDouble As Double

    aString = "Some Text"
    aDouble = 3.1429

    MyFileWriter.WriteLine(aString)
    MyFileWriter.WriteLine(aDouble)

    For i = 1 To 10
        MyFileWriter.WriteLine(i)
    Next

    MyFileWriter.Close()

请注意,在上面,文件不(不能)已经存在。

所以,你必须处理这个问题。

您可以在开始写入之前删除文件。

例如:

        Dim FILE_NAME As String = "C:\test\test2.txt"

    If File.Exists(FILE_NAME) Then
        File.Delete(FILE_NAME)
    End If

(接下来是您的其余代码。因此不再需要(或概念)FreeFile()。

输出:

Some Text
3.1429
1
2
3
4
5
6
7
8
9
10

编辑:-----------------------------

好吧,展示如何写出文件有点像拥抱,而不是亲吻。那么,现在,我们如何读取文件?

嗯,这真的取决于我们想用那个输入做什么。但是 .net 倾向于一次性阅读(或实际上是编写)整个内容。

但是,逐行读取将如下所示:

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

    Dim FILE_NAME As String = "C:\test\test2.txt"


    Dim MyFileReader As New StreamReader(FILE_NAME)

    Do While MyFileReader.EndOfStream = False

        Debug.Print("one line of file = " & MyFileReader.ReadLine)

    Loop

    MyFileReader.Close()


End Sub

output:
one line of file = Some Text
one line of file = 3.1429
one line of file = 1
one line of file = 2
one line of file = 3
one line of file = 4
one line of file = 5
one line of file = 6
one line of file = 7
one line of file = 8
one line of file = 9
one line of file = 10
' vb.net has allowed declaration and assignment on the same line for more than 20yrs now
Dim aString as String = "Some Text"
Dim aDouble as Double = 3.1429

' New API methods do not use integers to track files. 
' That was always kind of weird. 
Dim outpath As String = "C:\afolder\somedata.dat"

' StreamReader/StreamWriter are great, and you should learn them, 
'  but you don't need them for this sample
System.IO.File.WriteAllText(outpath, $"{aString}{vbCrLf}{aDouble}")
' Interpolated strings (like above) are kind of fun, too

同样,没有评论并假设 System.IOImports 指令,以强调这可能是多么简单:

Dim aString as String = "Some Text"
Dim aDouble as Double = 3.1429
Dim outpath As String = "C:\afolder\somedata.dat"
File.WriteAllText(outpath, $"{aString}{vbCrLf}{aDouble}")

如果插入的字符串太混乱,这里还有一个选项:

Dim aString as String = "Some Text"
Dim aDouble as Double = 3.1429
Dim outpath As String = "C:\afolder\somedata.dat"
File.WriteAllText(outpath, aString)
File.AppendAllText(outpath, aDouble.ToString("r"))

但是,实际上打开、写入和关闭文件两次。这是额外的 I/O,这是您在计算机上可以执行的最慢的操作,所以我不推荐它。

相反,您可以通过连接来构建输出字符串,但我相信您知道这也可能很麻烦,所以这里是最后一个替代方法 System.IO.StreamWriter:

Dim aString as String = "Some Text"
Dim aDouble as Double = 3.1429
Dim outpath As String = "C:\afolder\somedata.dat"

'Using blocks (added way back in 2005) replace the need to Close the file
' Moreover, they promise to dispose of the file handle
'  **even if there's an exception/error**,
'  making your code safer and better.
Using writer As New StreamWriter(outpath)
    writer.WriteLine(aString)
    writer.WriteLine(aDouble)
End Using 

然后读回值:

Dim filePath As String = "C:\afolder\somedata.dat"

Dim aString as String
Dim aDouble as Double
Using reader As New StreamReader(filePath)
    aString = reader.ReadLine()
    aDouble = Double.Parse(reader.ReadLine())
End Using