OK, I found a work-around. I have to use an additional, undocumented step.
Using fs As FileStream = New FileStream(thisFile, FileMode.Open)
Using reader As StreamReader = New StreamReader(fs)
fileText = reader.ReadLine()
End Using
End Using
I found examples of code online that used my earlier approach, so I am guessing
that the need to wrap the file into a FileStream is new, and Microsoft hasn’t
bothered to update the documentation, since all of their examples are using
hard-coded file paths, not variables.
The “Using” statements are a way to make Visual Basic handle the garbage
collection once the objects are no longer in scope.
From: [email protected] <[email protected]> On Behalf Of
[email protected]
Sent: Monday, November 25, 2019 11:53 AM
To: [email protected]
Subject: [nlug] [OT] Visual Basic Question
I am trying to code a Visual Basic 2019 console app that makes use of the
StreamReader object to read text from a file. According to the Microsoft
documentation for the StreamReader object, when it is initialized, you pass a
string with the path of the file to be opened, such as
Using reader as StreamReader = new StreamReader(“c:\sample.txt”)
However, all of the Microsoft documentation has the path hard-coded, not as a
variable. In my code, I am declaring a variable as type string, calculating
its value at runtime, and passing that string to StreamReader as an argument.
Dim thisFile as string
Using reader as StreamReader = new StreamReader(thisFile)
This results in a compile-time error, BC30311 Value of type ‘String’ cannot be
converted to ‘Stream’
What data type do I need to declare the variable as, instead of as a string,
for this to compile and work?
--
--
You received this message because you are subscribed to the Google Groups
"NLUG" group.
To post to this group, send email to [email protected]
<mailto:[email protected]>
To unsubscribe from this group, send email to
[email protected]
<mailto:[email protected]>
For more options, visit this group at
http://groups.google.com/group/nlug-talk?hl=en
---
You received this message because you are subscribed to the Google Groups
"NLUG" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected]
<mailto:[email protected]> .
To view this discussion on the web visit
https://groups.google.com/d/msgid/nlug-talk/002f01d5a3b9%24257ec580%24707c5080%24%40jfeldredge.com
<https://groups.google.com/d/msgid/nlug-talk/002f01d5a3b9%24257ec580%24707c5080%24%40jfeldredge.com?utm_medium=email&utm_source=footer>
.
--
--
You received this message because you are subscribed to the Google Groups
"NLUG" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nlug-talk?hl=en
---
You received this message because you are subscribed to the Google Groups
"NLUG" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/nlug-talk/00a901d5a3c5%244855e050%24d901a0f0%24%40jfeldredge.com.