Openfiledialog

zack

Well-known member
Joined
Jun 9, 2005
Messages
65
Programming Experience
Beginner
I'm trying to open a specified text file without having the program to open a dialog. how should I do it? The error is highlighted in red.

Imports System.IO

Public Class Form1
Inherits System.Windows.Forms.Form

Windows form generated codes

Private Sub btnSelectPicture_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelectPicture.Click

Dim myStream As Stream
Dim openFileDialog1 As New OpenFileDialog

If openFileDialog1.ShowDialog() = DialogResult.OK Then
myStream = openFileDialog1("\Program Files\GPS.txt")

C:\Documents and Settings\user\My Documents\Visual Studio Projects\openfile\Form1.vb(82): Class 'System.Windows.Forms.OpenFileDialog' cannot be indexed because it has no default property.

If Not (myStream Is Nothing) Then
' Insert code to read the stream here.
myStream.Close()
End If
End If
End Sub
Private Sub binQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuit.Click

Me.Close()
End Sub
End Class
 
The OpenFileDialog does not open a file. It simply provides an interface suitable for a user to select a file to be opened. The path of the file they select is then found in the FileName property of the OpenFileDialog. You then use this file path as you see fit. In your case, I'd say you want to pass it to the constructor of a StreamReader object, which is generally used to read text files. If you know what file you want to open then you don't need the OpenFileDialog. As I said, that is so the user can select a file. If you have the path already, just go ahead and open it with a StreamReader.
 
I've amended the codes to a StreamReader:

Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
Dim S As Stream
S = File.OpenRead("My Documents\Personal\GPS.txt")
End Sub
I'm very sure the path is correct but the file does not open. Could you tell me why? Thanks!
 
"My Documents" is not a real folder. It is just an alias, usually for "C:\Documents and Settings\<your user name>\My Documents". To save you knowing the full path, which may change from machine to machine and user to user, you can use this:
VB.NET:
[size=2]S = File.OpenRead(Environment.GetFolderPath(Environment.SpecialFolder.Personal) & "\Personal\GPS.txt")[/size]
There are various other special folders that you can access in the same fashion. Take a look at the Environment.SpecialFolder enumeration to see what they are.
 
JM is absolutely right. Also, note that you should always refer to the absolute path of the file )if you not use .NET exposed methods) ... WinXP usually has a default path for myDocuments folder:
C:\Documents and Settings\ComputerName\My Documents so, you can also read the file as it follows:

VB.NET:
S = File.OpenRead("C:\Documents and Settings\ComputerName\My Documents\Personal\GPS.txt")


Cheers ;)
 
Hi, I've really tried very hard and find all codes that can read my file. But none of them is able to do that. It can't read nor write the information out from my file. PLEASE HELP!!
s10.gif
I'm using a pocketPC to do this...

Dim sr As StreamReader = New StreamReader("\Program Files\GPS.txt")
Dim line As String
' Read and display the lines from the file until the end
' of the file is reached.
' Do
line = sr.ReadLine()
Console.WriteLine(line)
' Loop Until line Is Nothing
' sr.Close()

This is another type of streamReader code that i found... But the program is still unable to read the contents of the file...
 
I'm not 100% sure how file paths are defined on PPC, but assuming that the path you are passing is valid then you would use code like this:
VB.NET:
		Dim myReader As New IO.StreamReader("\Program Files\GPS.txt")

		While myReader.Peek() <> -1
			MessageBox.Show(myReader.ReadLine())
		End While

		myReader.Close()
All the methods I've used are supported on the CF.
 
Thanks jmcilhinney!

It finally worked! Thanks alot to you! :D

But it reads only 1 line. How do you make it read multiple lines?
 
The code I provided will keep reading lines from the file until there are no more. Check the help topic for the Peek method to see what it does. You will probably want to assign each line to a variable as it is read rather than just display it.
 
Hi, I've deployed the codes into my pocketPC. They work well. But the problem is the streamreader keeps reading and does not stop, causing my other programs not to function. Can anyone help?
 
Do you mean that the reader continues indefinitely, or that it does finish but while it's reading everything else becomes unresponsive?

As I said, the code I provided will read the file one line at a time until it reaches the end of the file, at which point the file is closed. If the process of reading the file is causing your PPC to become unresponsive then you either need to accept that and wait until it is finished or you can perform the file read in a seperate thread. If the loop that reads the file never ends then I think you may have done something wrong. In that case I think you need to post the catual code you are using.
 
Hi, I found out that the problem lies in my event. It actually loops back to run the streamReader. Is there a way which i can stop an event/part of the program fromo running when i press a button?
 
An event is always raised when the prescribed "thing" happens, like the Click event being raised when you click a Button. If and how you choose to handle that event is up to you. Of course you can put conditional statements in your event handlers so that certain code is not executed when certain conditions are met.
 
Back
Top