assigning file contents to a string

  • Thread starter Thread starter Mark
  • Start date Start date
M

Mark

For purposes of sending the contents of a file to a function that requires a
string, I am trying to find a way to read a file to a string variable & pass
it along.

For example, if the file text.txt contained "this is a test" I couldn't just
do this:
strVar = "text.txt"
because the string variable would obviously represent just the filename &
not it's contents.

I am sorry if this is very simple, but of course I'm new to VB (& ASP) and
any suggestions would be great.
Thanks.
 
* "Mark said:
For purposes of sending the contents of a file to a function that requires a
string, I am trying to find a way to read a file to a string variable & pass
it along.

For example, if the file text.txt contained "this is a test" I couldn't just
do this:
strVar = "text.txt"
because the string variable would obviously represent just the filename &
not it's contents.

\\\
Dim sr As New System.IO.StreamReader("C:\bla.txt")
Dim s As String = sr.ReadToEnd()
sr.Close()
///
 
Mark said:
For purposes of sending the contents of a file to a function that
requires a string, I am trying to find a way to read a file to a
string variable & pass it along.

For example, if the file text.txt contained "this is a test" I
couldn't just do this:
strVar = "text.txt"
because the string variable would obviously represent just the
filename & not it's contents.

I am sorry if this is very simple, but of course I'm new to VB (&
ASP) and any suggestions would be great.
Thanks.


http://msdn.microsoft.com/library/en-us/vbcn7/html/vbconFolderFileProcessing.asp
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconworkingwithio.asp
 
Try
' Create an instance of StreamReader to read from a file.
Dim sr As StreamReader = New StreamReader("TestFile.txt")
Dim line As String
' Read and display the lines from the file until the end
' of the file is reached.
Do
line = sr.ReadLine()
Console.WriteLine(Line)
Loop Until line Is Nothing
sr.Close()
Catch E As Exception
' Let the user know what went wrong.
Console.WriteLine("The file could not be read:")
Console.WriteLine(E.Message)
End Try
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top