OK i am new to .NET

Jay1b

Well-known member
Joined
Aug 3, 2003
Messages
640
Location
Kent, Uk.
I am trying to read in te contents of a file to a variable and then pass it on to a textbox, using the code below. However it doesnt seem to work, OpenMode Append or Output i get the error message below. Where as with binary it just freezes.

An unhandled exception of type System.IO.IOException occurred in microsoft.visualbasic.dll

Additional information: Bad file mode.



Private Sub ActivateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ActivateButton.Click
Dim GetTxtFile As String test variable for file get

Writes to file
FileOpen(1, "c:\test.txt", OpenMode.Binary, OpenAccess.Write)
Print(1, "Hello", SPC(20), "Bye")
FileClose(1)

Retrieves the contents of the file to the variable name
FileOpen(2, "c:\test.txt", OpenMode.Binary)
FileGet(2, GetTxtFile)
FileClose(2)

DisplayText.Text = GetTxtFile

End Sub
End Class

Can anyone help me please?
 
You shouldnt use the old VB6 methods. Use System.IO.StreamReader class for reading files and System.IO.StreamWriter for writing to files.
Here is how you can a file into a variable and then assign it to a textbox:
Code:
Dim reader As New System.IO.StreamReader("path to the file")
Dim textfromfile As String = reader.ReadToEnd() use the ReadToEnd()
method to read the whole file
TextBox1.Text = textfromfile assign the variable to the textboxs text
 
Last edited by a moderator:
Youll need to use IO.BinaryReader to read a binary file. StreamReader wont do it properly I think.
 
Ah, sorry, I didnt notice that you were using binary access. Thanks VolteFace.
Code:
Dim reader As New IO.BinaryReader(New IO.FileStream("Path to the file", IO.FileMode.Open))
new binary reader, you have to create a filestream as the BinaryReader
itself doesnt take file paths but streams
then you can read
reader.ReadChar() or other methods like that
reader.Close() close the stream
 
Thank you, but is there any reason why i shouldnt keep using the old VB way? Are they taking it out of the next version or something?

Also how do i know what to read it in as binary or stream? At the moment i am just reading in a textfile - or trying to [:)]
 
The older way is less functional, less extensible, slightly slower and incompatible with other .Net languages.
If you need to use 3rd party code / dlls that are based around the classes in System.IO then you would have to recode ALL your old-style vb6 file handling code.
 
Originally posted by Jay1b

Also how do i know what to read it in as binary or stream? At the moment i am just reading in a textfile - or trying to [:)]

If you are just reading a plain text file then use the IO.StreamReader class.
:)
 
What would i use if i was reading in a picture or a word doc? Is the basic code the same - obvious a picture wouldnt go to a txtbox, but otherwise...........

Thanks.
 
Do you want to put the picture in a picturebox? If yes then this is how you can load it:
Code:
PictureBox1.Image = Image.FromFile("path to the image")
use a shared method of the Image class to load the picture

I believe that Word documents are encoded and simply reading them wont give you good results.
 
Back
Top