[2005] open file dialog problem
I am trying to use the open dialog to allow the user to select the file being computed. I think my code is right, but visual basic says that 'type stream has not been defined'.
Any help is appreciated. Code below.
champ0342
Code:
Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim myStream As Stream = Nothing
Dim ofd As New OpenFileDialog()
ofd.InitialDirectory = "c:\"
ofd.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
If ofd.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Try
myStream = ofd.OpenFile()
If (myStream IsNot Nothing) Then
End If
Catch Ex As Exception
MessageBox.Show("Cannot read file from disk. Try another file)
Finally
If (myStream IsNot Nothing) Then
End If
End Try
End If
End Sub
Re: [2005] open file dialog problem
Dim fileStream As System.IO.FileStream = Nothing
try that
Re: [2005] open file dialog problem
Thanks. That kinda did it, but I had to change one part:
Code:
Dim fileStream As System.IO.Stream = Nothing
by making it system.io.stream instead of system.io.filestream it works under option strict.
Re: [2005] open file dialog problem
try this
Code:
Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim ofd As New OpenFileDialog()
Dim MyValue As String = ""
ofd.InitialDirectory = "c:\"
ofd.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
If ofd.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Using sr As New StreamReader(ofd.FileName)
MyValue = sr.ReadToEnd()
End Using
End If
End Sub
That should work, and if the file is not found
use ofd.CheckFilePath, or a property with a similar name which checks the file exists...
Re: [2005] open file dialog problem
A new challenger approaches.....
when it opens the file it gives me an error says that ' the system cannot find the file .../bin/debug/system.io.filestream'
does anyone know what that error means?
Re: [2005] open file dialog problem
think its HAS TO BE
system.io.FILESTREAM
and not system.io.stream
since you will be using all the funcitons that deals with FILES.
Re: [2005] open file dialog problem
Sorry, I forgot to mention, you need to do this at the top of your code (above everything)
Imports System.IO
or change the streamreader code to
Code:
Using sr As New System.IO.StreamReader(ofd.FileName)
MyValue = sr.ReadToEnd()
End Using
Re: [2005] open file dialog problem
I need to split the line into two sections. How do I do that. I tried this code:
Code:
MyValue = sr.ReadLine.Split
but it said that: "value of a 1d array can not be converted to a string".
Re: [2005] open file dialog problem
Declare MyValue As An Array
Dim MyValue() As String
MyValue = sr.ReadLine.Split()