vb.net VB读取文本文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22358290/
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-17 17:03:27  来源:igfitidea点击:

Read a text file in VB

vb.net

提问by Colby Bradbury

New Visual basic programmer here. Trying to get a textfile to be read by the program but it seems to simply not work, no error messages or anything. It just does not grab the values at all

新的 Visual Basic 程序员在这里。试图让程序读取一个文本文件,但它似乎根本不起作用,没有错误消息或任何东西。它根本没有抓住价值

The textfile name is exactly the same.

文本文件名完全相同。

Public Sub ReadDef()

    Dim DefSR As IO.StreamReader = IO.File.OpenText("BikeDefault.txt")

    GlobalTotBikes = DefSR.ReadLine()
    GlobalRentRate = DefSR.ReadLine()
    GlobalHSTRate = DefSR.ReadLine()
    GlobalTourRate = DefSR.ReadLine()
    GlobalGPSRate = DefSR.ReadLine()
    GlobalInsurRate = DefSR.ReadLine()
    GlobalWaterBotRate = DefSR.ReadLine()
    GlobalNextBookNum = DefSR.ReadLine()
    GlobalNextCustNum = DefSR.ReadLine()
    GlobalNextInvoiceNum = DefSR.ReadLine()

    DefSR.Close()

End Sub

I've compared this code a bunch of times to the example I was given and I see nothing different.

我将此代码与给出的示例进行了多次比较,但没有发现任何不同。

Thanks.

谢谢。

回答by Dayan

Simple search on google http://www.dotnetperls.com/streamreader-vbnet

在谷歌上简单搜索http://www.dotnetperls.com/streamreader-vbnet

Make 100% sure BikeDefault.txtexists. If you wish to make sure, copy the file over to the C:\Drive to keep it simple and replace your BikeDefault.txtwith "C:\\BikeDefault.txt"

确保 100%BikeDefault.txt存在。如果您想确定,请将文件复制到C:\云端硬盘以保持简单并替换BikeDefault.txt"C:\\BikeDefault.txt"

You can use the StreamReader like so:

您可以像这样使用 StreamReader:

Imports System.IO

Module Module1

    Sub Main()
    ' Store the line in this String.
    Dim line As String
    Dim FilePath As String = "C:\BikeDefault.txt"
    ' Create new StreamReader instance with Using block.
    Using reader As StreamReader = New StreamReader(FilePath)
        ' Read one line from file
        line = reader.ReadLine
    End Using

    ' Write the line we read from "file.txt"
    Console.WriteLine(line)
    End Sub

End Module

Or keep it simple with File.ReadAllLines.

或保持简单File.ReadAllLines

For Each line As String In File.ReadLines("MyTextFile.txt")
    'Code here to read each line
Next line