How to load a text file (ANSI) on a string variable

Mansour_Dalir 2,036 Reputation points
2023-09-16T17:06:18.14+00:00

Encoding ANSI.txt Size: (1,364 bytes) update 1

I can't load the whole file.

It cannot be recognized from this part.: Subj (þÿ/1 DÌ3* '(2'1 /BÌB EH,H/ FÌ3* . 33D'E)

 Dim sttLoad As String
sttLoad = File.ReadAllText("c:\Encoding ANSI.txt", System.Text.Encoding.Default)
'Unfortunately, the upload is incomplete
'The string length is much less than (1,364 bytes).
Developer technologies VB
{count} votes

Accepted answer
  1. WayneAKing 4,931 Reputation points
    2023-09-16T21:15:55.0333333+00:00

    >This was the original of my file. Original File.txt file format is '.fdf' 'PDF Comment File'

    Care must be taken to ensure that the file being read is in fact a text file. That is, it does not contain binary data that may be misinterpreted if read and processed as part of a text file. The contents of the sample files you provided appear to suggest that these are not simple text files. If that is the case, then they probably should be read and processed as binary files. For example, using ReadAllBytes rather than ReadAllText:

    File.ReadAllBytes(String) Method

    https://learn.microsoft.com/en-us/dotnet/api/system.io.file.readallbytes?view=net-7.0

    Generally the text processing file routines are not reliable when unexpected binary characters are encountered.

    File and Stream I/O

    https://learn.microsoft.com/en-us/dotnet/standard/io/

    "TextReader and TextWriter - serve as the abstract base classes for other readers and writers that read and write characters and strings, but not binary data."

    • Wayne

1 additional answer

Sort by: Most helpful
  1. WayneAKing 4,931 Reputation points
    2023-09-16T18:13:14.6166667+00:00

    I concur with Les's assessment. When I check the length of the String in a Debug session it shows as 1348. See image attached.

    Note that if you are looking at a visualization of the String contents, as shown in some Debug displays of the IDE, the whole String may not be shown depending on its length. This will be indicated by the presence of an ellipsis at the end of the display.

    String Length


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.