Jay1b Posted August 17, 2003 Posted August 17, 2003 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? Quote
*Experts* mutant Posted August 17, 2003 *Experts* Posted August 17, 2003 (edited) 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: 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 textbox's text Edited August 17, 2003 by mutant Quote
*Experts* Volte Posted August 17, 2003 *Experts* Posted August 17, 2003 You'll need to use IO.BinaryReader to read a binary file. StreamReader won't do it properly I think. Quote
*Experts* mutant Posted August 17, 2003 *Experts* Posted August 17, 2003 Ah, sorry, I didnt notice that you were using binary access. Thanks VolteFace. 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 Quote
Jay1b Posted August 18, 2003 Author Posted August 18, 2003 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 [:)] Quote
Administrators PlausiblyDamp Posted August 18, 2003 Administrators Posted August 18, 2003 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
*Experts* mutant Posted August 18, 2003 *Experts* Posted August 18, 2003 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. :) Quote
Jay1b Posted August 19, 2003 Author Posted August 19, 2003 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. Quote
BryanZM Posted August 19, 2003 Posted August 19, 2003 use filestream Use the filestream class, it makes it very easy to read in a file. Quote
*Experts* mutant Posted August 19, 2003 *Experts* Posted August 19, 2003 Do you want to put the picture in a picturebox? If yes then this is how you can load it: 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. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.