Question

I'm pretty new to visual basic (and coding in general) so if I've made any really simple mistakes let me know.
Right now, I'm getting a pretty weird problem with my vb.net code. The filestream is able to correctly open the file and read from it - but what's weird is that while the code is able to read a bunch of lines from the beginning of the file, when I manually open the file in notepad I'm not. Here's the code:

    Dim fs, f, s 'filesystemobject, file, stream.
    fs = CreateObject("Scripting.FileSystemObject")
    f = fs.GetFile(CurrDataPath) ' This change made to ensure the correct file is opened
    s = f.OpenAsTextStream(1, 0) ' 1 = ForReading, 0 = as ASCII (which i think is right?)
    Dim param(14) As String
    Dim line As String
    line = s.ReadLine()
    While i <= 14
        i += 1
        MessageBox.Show(line)
        line = s.ReadLine()
    End While

(I've read that arrays are a bad idea but they've been convenient and haven't caused me any problems so I've been using them anyways.)
What's weird is that when this code is run, it will (in the message boxes) show me the information I want to see - which isn't bad at all. The information that I want looks like this:

BEGINPARAM  
parameter1, 0  
parameter2, 7.5  
ENDPARAM

EDIT: After using Path.GetFullPath(DFile), I found that there were two files in different directories with the same name DFile. The file I had been opening in Notepad was saved in the directory where I expected it to be saved, while the file the code was reading was saved in the VB project's folder.
Once I changed the code to rely on CurrDataPath which includes the expected path, the code read from the file exactly what I did in notepad.
I do have word wrap on in notepad, so I know that's not the issue, however, I will look into getting notepad++.
The file named DFile is created in a c++ program that I'll be digging through to find out why one part of the file is written to a different folder than the rest.

Obviously I'm missing something important, and if anyone could help, that would be great.
*Note: This is a vb6 migration project so if anyone asks I can provide the old code.

Was it helpful?

Solution

Assuming the most recent version of VB.Net, the modern way to write that is like this:

For Each line As String In File.ReadLines(CurrDataPath).Take(14)
   MessageBox.Show(line)
Next

OTHER TIPS

I'm not 100% clear on what you're saying. There's nothing in this code that outputs to a file, so what you have to be saying is that when you open the file referenced by "DFile" on line 3 above, that file doesn't have the lines containing "parameter1, 0" and "parameter2, 7.5" in it?

Since we know that's not technically possible, do verify the answer to the question above and make sure you're really opening the same file in notepad as the script is opening. The second thing to do is to turn on Word Wrap in Notepad or download Notepad++ (a text editor I think everyone should have anyway) and make sure that the data's actually missing, and not just not showing on your screen because it's not using Windows style line endings.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top