VB.net 无法正确读取文本文件

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

VB.net will not read text file correctly

vb.netencoding

提问by Mystx

I've been trying to use StreamReaderto read a log file. I cannot verify what it is encoded in, as when I open it in notepad++ and select ANSI encoding, I get this result:

我一直在尝试使用StreamReader读取日志文件。我无法验证它的编码内容,因为当我在记事本++中打开它并选择 ANSI 编码时,我得到以下结果:

screenshot

截屏

I'm getting the characters needed when using ANSI but they are followed by things like [NULL][EOT][SOH][NUL][SI]

我在使用 ANSI 时得到了所需的字符,但后面跟着 [NULL][EOT][SOH][NUL][SI] 之类的东西

When I try and read the file in VB (using StreamReaderor ReadAll) with ANSI encoding selected the resulting string I get back is completely wrong.

当我尝试在 VB 中读取文件(使用StreamReaderReadAll)并选择 ANSI 编码时,我得到的结果字符串完全错误。

How could I read a file like this in VB.net?

我怎么能在 VB.net 中读取这样的文件?

采纳答案by Sam

You could use the IO.File.ReadAllText("File Location", encoding as System.Text.Encoding)method,

您可以使用IO.File.ReadAllText("File Location", encoding as System.Text.Encoding)方法,

Dim textFromFile as string = IO.File.ReadAllText("C:\Users\Jason\Desktop\login20130417.rdb", System.Text.Encoding.ASCII) 'Or Unicode, UFT32, UFT8, UFT7, BigEndianUnicode or default. Default is ANSI.

If you still don't get the text you need by using the default encoding (ANSI), then you can always try the other 6 different encoding methods.

如果使用默认编码 (ANSI) 仍然没有得到您需要的文本,那么您可以随时尝试其他 6 种不同的编码方法。

Update...

更新...

It appears that your file is corrupt, using the code below I was able to get a binary representation of whatever is in the file, I got this,

看来您的文件已损坏,使用下面的代码我能够获得文件中任何内容的二进制表示,我得到了这个,

1111111111111101000001110000010000000000000001010000000000010011000000000000100000000000000111100000000000100110000000000011100000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111111111111110100000111000001000000000000000101000000000001001100000000000010000000000000011110000000000010100000000000111111111111110100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000110011100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

The massive amount of null data would suggest that the file is corrupt, which would also explain why we are not getting a lot of data whenever we try to read the file.

大量的空数据表明文件已损坏,这也可以解释为什么每次尝试读取文件时我们都没有获得大量数据。

The code,

编码,

Dim fileData As String = IO.File.ReadAllText("C:\Users\Jason\Desktop\login20130417.rdb")
Dim i As Integer = 0
Dim binaryData As String = ""
Dim ch As String = ""
Do Until i = fileData.Length
   ch = fileData.Chars(i)
   bin = bin & System.Convert.ToString(AscW(ch), 2).PadLeft(8, "0")
   i = i + 1
Loop

回答by AJ.

As @Daniel A. White suggested in his comment, that file does not appear to be encoded like a "normal" text file. A StreamReaderwill not work in this situation. I would attempt to use a BinaryReader.

正如@Daniel A. White 在他的评论中所建议的那样,该文件似乎不像“普通”文本文件那样被编码。AStreamReader在这种情况下不起作用。我会尝试使用BinaryReader

回答by Michael Parr

Rdb file? Never heard of it. Quick google makes it less clear - n64 database file, Darkbot, etc...

.rdb 文件?从来没有听说过。快速谷歌让它不太清楚 - n64 数据库文件,Darkbot 等......

However considering the name you have, and the general look of the opened file, i would say its a binary file.

然而,考虑到你的名字,以及打开文件的一般外观,我会说它是一个二进制文件。

If you want to read the file in vb.net you'll need a library of sorts, and i can't help you with one until you are able to shed some light on what the file may be, or what it was created with.

如果你想在 vb.net 中阅读文件,你需要一个各种库,在你能够弄清楚文件可能是什么,或者它是用什么创建的之前,我无法帮助你.