这里是新的 Visual basic 程序员。试图让程序读取一个文本文件,但它似乎根本不起作用,没有错误消息或任何东西。它只是根本不获取值
文本文件名完全相同。
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
我已经多次将此代码与我收到的示例进行比较,没有发现任何不同。
谢谢。
最佳答案
在谷歌上简单搜索http://www.dotnetperls.com/streamreader-vbnet
100% 确定 BikeDefault.txt
存在。如果您想确定,请将文件复制到 C:\
驱动器以保持简单,并将 BikeDefault.txt
替换为 "C:\\BikeDefault.txt”
您可以像这样使用 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
或者使用 File.ReadAllLines
保持简单。
For Each line As String In File.ReadLines("MyTextFile.txt")
'Code here to read each line
Next line
关于vb.net - VB读取文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22358290/