我正在尝试读取我用单独的程序成功编写的.txt文件,但我一直让程序停止(也就是根本没有输入/输出,就像它有一个无限循环之类的东西)。我收到消息"A",但没有其他消息。
我在这样的网站上看到过很多这样的帖子,列出了各种创造性的读取文件的方法,但我找到的每个指南都希望我在Msgbox A和Msgbox D之间更改代码。没有一个改变结果,所以我开始认为问题实际上是我指出文件位置的方式。有一个代码(与Dim objReader As New System.IO.TextReader(FileLoc)有关),但当我请求读取文件时,我得到了文件的地址。这就是为什么我怀疑我指向的.txt是错误的。有一个问题..。
如果我做的是错的,我完全不知道该怎么做。
我在末尾附加了代码片段(其中的每一行无关数据都被剔除)。
如果重要的话,实际程序的位置在"G01-Cartography“文件夹中。
Private Sub GameMain_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
LoadMap("Map_Cygnus.txt")
End Sub
Private Sub LoadMap(FileLoc As String)
FileLoc = "C:\Users\Adam\Documents\Visual Studio 2013\Projects\G01-Cartography\Maps\" + FileLoc
MsgBox("A")
Using File As New StreamReader(FileLoc)
MsgBox("B")
Dim WholeMap = File.ReadLine()
MsgBox("C")
End Using
MsgBox("D")
End Sub发布于 2015-04-14 11:19:19
根据MSDN,看起来您使用的方法/对象是正确的。您的代码可以在新的VB控制台应用程序(.net 4.5)中为我运行
与MSGBOX不同的一种方法是使用Debug.WriteLine或Console.WriteLine。
- Probably you are watching the application for output but the debugger(visual studio) has stopped the application on that line, with an exception. eg File not found, No Permission, using a http uri...
- Permissions?
- Does it have a Line of Text?
- Is the folder 'online'
WholeMap MsgBox("C")连接到MSGBOX会显示什么内容
发布于 2015-04-14 11:34:21
运行此命令会在调试器中显示什么内容?您可以在记事本中打开Map_Cygnus.txt文件吗?在第一行设置一个断点,然后运行程序来查看发生了什么。
Private BaseDirectory As String = "C:\Users\Adam\Documents\Visual Studio 2013\Projects\G01-Cartography\Maps\"
Private Sub GameMain_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim WholeMap = File.ReadAllText(Path.Combine(BaseDirectory, "Map_Cygnus.txt"))
Debug.Print("Size Of Map: {0}", WholeMap.Length)
End Sub发布于 2015-04-14 20:34:52
我有几个建议。首先,使用Option Strict On,它将帮助你避免以后的头痛。
打开文件的代码是正确的。除了避免使用MsgBox()进行调试而改为设置断点或使用Debug.WriteLine()之外,还应将该子例程包装在Try...Catch异常中。
Private Sub GameMain_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
LoadMap("Map_Cygnus.txt")
End Sub
Private Sub LoadMap(FileLoc As String)
Try
FileLoc = "C:\Users\Adam\Documents\Visual Studio 2013\Projects\G01-Cartography\Maps\" + FileLoc
MsgBox("A")
Using File As New StreamReader(FileLoc)
MsgBox("B")
Dim WholeMap = File.ReadLine() 'dimming a variable inside a block like this means the variable only has scope while inside the block
MsgBox("C")
End Using
MsgBox("D")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub请注意,通常您应该只捕获您期望的任何异常,但我通常会在调试此类事件时捕获所有异常。
我还想指出的是,您只将文件中的一行读取到变量WholeMap中。一旦命中End Using行,该变量就会失去作用域,从而丢失您刚刚从文件中读取的行。我假设你以这种方式获得了代码,因为它似乎给你带来了阅读它的麻烦,但我想我还是要指出它。
Public Class GameMain
Private WholeMap As String = ""
Private Sub GameMain_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
LoadMap("Map_Cygnus.txt")
End Sub
Private Sub LoadMap(FileLoc As String)
Try
FileLoc = "C:\Users\Adam\Documents\Visual Studio 2013\Projects\G01-Cartography\Maps\" + FileLoc
Using File As New StreamReader(FileLoc)
WholeMap = File.ReadLine() 'dimming the variable above will give all of your subs inside class Form1 access to the contents of it (note that I've removed the Dim command here)
End Using
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
End Classhttps://stackoverflow.com/questions/29617772
复制相似问题