filedialog troubles
Hi everyone...having real trouble with this openfiledialog code:
Private Sub button2_Click(ByVal sender AsObject, ByVal e As System.EventArgs) Handles Button2.Click
Dim openFileDialog1 AsNew OpenFileDialog()
openFileDialog1.InitialDirectory = "o:\"
openFileDialog1.Filter = "excel files (*.xls)|*.xls|autocad files (*.dwg)|*.dwg|all files (*.*)|*.*"
openFileDialog1.RestoreDirectory = True
openFileDialog1.Multiselect = True
If openFileDialog1.ShowDialog = DialogResult.OK Then
openFileDialog1.OpenFile()
End If
End Sub
the open file box opens, youcan select the type of file you want and then when you hit open...nothing. File does not open?
If you could help it would be hugely appreciated.
Thanks
Adam.
[776 byte] By [
adamgeck] at [2007-11-20 10:26:01]

# 1 Re: filedialog troubles
This is a .Net question? If .Net is same as VB6, then the OpenFile command does not open a file, it shows the Open File dialog, allowing you to select a file or file(s). Once a file is selected and you get the file name from the control, then you have to execute the code to actually open the file.
# 3 Re: filedialog troubles
You'd want ShellExecute in VB6, and a similar statement in VB.Net.
If it's not a VB6 question, then search the other forum. I'm sure it's there.
# 4 Re: filedialog troubles
Look at the 50 most frequently asked questions. The sticky post in this forum. ShellExecute is the answer to the 3rd question (VB6 though). I haven't visited the .Net forum, but as David recommended, it is most likely there too.
# 6 Re: filedialog troubles
Adam, what do you want to open
As stated, with the OpenFileDialog you only are capable of selecting a certain filename - that's it. You'd have to write the whole logic of the actual opening of the file, if that is what you want to do.
Let's assume you have a textbox and you want to load a Text file into it. Now, you can use the StreamReader to read the contents of the file, and load it into the textbox.
If you are using the OpenFileDialog, just to select a file ( for example a Picture file ), then you'd need to store the FileName of the file that was selected, then ( again ) write the logic of loading the Picture file or what ever.
If you want to open a file in its Default program, for example: you have a Word document and you want to open in in Microsoft Word, then, you can use ShellExecute, BUT, all of this depends on exactly what you want to do :)
# 7 Re: filedialog troubles
You are opening the file. You just aren't doing anything with it. The OpenFile method of the OpenFileDialog class returns a FileStream object that you can use to read the contents of the file. You're calling the method and then simply discarding the FileStream it returns. This:If openFileDialog1.ShowDialog = DialogResult.OK Then
openFileDialog1.OpenFile()
End Ifshould be something like this:If openFileDialog1.ShowDialog = DialogResult.OK Then
Dim stream As IO.FileStream = openFileDialog1.OpenFile()
Using reader As New IO.StreamReader(stream)
MessageBox.Show(reader.ReadToEnd(), "File Contents")
End Using
End IfAlternatively you can use the select file name and open the file any way you want, e.g.If openFileDialog1.ShowDialog = DialogResult.OK Then
MessageBox.Show(IO.File.ReadAllText(openFileDialog1.FileName), "File Contents")
End If