无法读取VB.NET文本文件的内容 [英] Cannot read contents of VB.NET text file
问题描述
Re:VB .Net查询(Visual Studio 2017版本15.7.5)
亲爱的所有
我很抱歉,但请您帮忙吗?
我正在尝试检查使用Visual Studio 2017写入光盘的文本文件是否已写入数据。当我打开文本文件时,要检查记事本文本文件的内容,它是空的。 names.txt文件位于我的Windows 10硬盘上。
请您提供建议,请看下面的内容?
谢谢。
祝你好运
< u> 以下脚本
Re: VB .Net query (Visual Studio 2017 version 15.7.5)
Dear All
I am sorry to ask, but would be so grateful for your help please?
I am trying to check whether a text file that has been written to disc, using Visual Studio 2017, has had data written to it. When I open the text file, to check the contents of the Notepad text file, it is empty. The names.txt file is on my Windows 10 hard drive.
Please could you advise and kindly see below?
Thank you.
Best wishes
Script below
Imports System.IO
Public Class Form1
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim names As String() = New String() {"Person 1", "Person 2", "Person 3", "Person"}
Dim s As String
Using sw As StreamWriter = New StreamWriter("C:\Users\user\source\repos\WindowsApp - Examples\WindowsApp5\names.txt")
For Each s In names
sw.WriteLine(s)
Next s
sw.Close()
End Using
' Read and show each line from the file.
Dim line As String
Using sr As StreamReader = New StreamReader("C:\Users\user\source\repos\WindowsApp - Examples\WindowsApp5\names.txt")
line = sr.ReadLine()
While (line <> Nothing)
MessageBox.Show(line)
line = sr.ReadLine()
End While
sr.Close()
End Using
End Sub
End Class
我尝试过:
亲爱的所有人,
我将读写文本文件代码分成不同的子程序并进行读取工作。但是,当我打开写入数据的文本文件时,内容为空。我很抱歉地问。
谢谢
What I have tried:
Dear All,
I have separated the reading and writing text file code into different subroutines and the reading works. However, when I open the text file where the data had been written to, the contents are empty. I am so sorry to ask.
Thank you
推荐答案
当我创建一个新项目并将代码放入其中时:
When I create a new project and drop your code into it:
Imports System.IO
Public Class Form1
Private Sub MyButton_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim names As String() = New String() {"Person 1", "Person 2", "Person 3", "Person"}
Dim s As String
Using sw As StreamWriter = New StreamWriter("D:\Temp\names.txt")
For Each s In names
sw.WriteLine(s)
Next s
sw.Close()
End Using
' Read and show each line from the file.
Dim line As String
Using sr As StreamReader = New StreamReader("D:\Temp\names.txt")
line = sr.ReadLine()
While (line <> Nothing)
MessageBox.Show(line)
line = sr.ReadLine()
End While
sr.Close()
End Using
End Sub
End Class
它的工作原理是:我得到四个MessageBoxes,每个字符串一个。
唯一的区别是我们的代码是输出文件的路径:我将它发送到我的临时文件夹,然后将其发送到 Users
目录下的文件夹。
所以我怀疑它是一个文件访问权限问题,你实际上是在收到一个错误,你正在吞咽或忽略某种方式。
在行上放置一个断点
It works: I get four MessageBoxes, one for each string.
The only difference between our code is the path to the output file: I sent it to my temporary folder, you send it to a folder under your Users
directory.
So I suspect it's a file access permissions problem and that you are actually getting an error that you are swallowing or ignoring somehow.
Put a breakpoint on the line
Dim names As String() = New String() {"Person 1", "Person 2", "Person 3", "Person"}
并在调试器中运行您的代码。当它到达断点时,您的应用程序将停止,并将控制权交给您。单步执行每一行并跟踪发生的情况 - 猜测代码根本没有执行,或者由于您吞下或忽略错误而无法完成。
and run your code in the debugger. When it hits the breakpoint, your app will stop, and hand control to you. Single step each line and follow what is happening - At a guess either that code isn't being executed at all, or it doesn't complete due to an error you swallow or ignore.
这篇关于无法读取VB.NET文本文件的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!