Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

  • *Experts*
Posted (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 by mutant
  • *Experts*
Posted

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

Posted

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 [:)]

  • Administrators
Posted

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.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

  • *Experts*
Posted

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.

:)

Posted

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.

  • *Experts*
Posted

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...