Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Browse for a file

Status
Not open for further replies.

trk1616b

MIS
May 25, 2005
20
0
0
US
Is there a way with vb.net to browse for a file on the computer? I want to make a button the user clicks and it brings up Microsoft's normal browsing windows so they can browse and select a file off their C drive. Any ideas?

Thanks.
 
Try using "open file dialog "

Add the OpenFileDialog form to your design.

Your code could be as simple as follows:

Private Sub btnClick_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnInvoices.Click

ofdImport.ShowDialog()

End Sub
 
ofdImport" is the name I gave to the OpenFileDialog form!
 
Thanks for the quick response, Shorty.

I can't find the 'OpenFileDialog' form you mentioned. I'm assuming it's a from already within VB, but I don't see it anywhere. Where can I find it?

Thanks again.
 
In the toolbox there is the OpenFileDialog...

Even if do do not drag one in the form (appears in the tray) you can simply:

Dim OpenOneFile as OpenFileDialog
OpenOneFile.ShowDialog()

etc...

If there is a problem post to help
 
It usually is at the bottom of the toolbox.
Else right click on it, select "add/remove items", in the ".Net framework components" (selected by default) check the "OpenFileDialog":
NameSpace: System.Windows.Forms



-
 
EXCELLENT! You guys are great...I was looking for Windows Forms in the Add Items from the top tool bar instead of the toolbox....nice work, thanks!

A couple quick questions while I have your attentions. My next task is to take the take the file selected and rename it. If I use "ofdImport.FileName", it displays the entire path (which I'm displaying). But is there a good way to
1) just get the file name
2) rename the file with a string i collected elsewhere?

Thanks!
 
1)

OpenFileDialog1.ShowDialog()

Dim theFileName As String = OpenFileDialog1.FileName
MessageBox.Show(theFileName.Substring(theFileName.LastIndexOf("\") + 1))
 
2)

Take a look at System.IO.File.Move("...","....")
 
Beautiful...at first try this seems to work like a charm. Thanks Tip and Shorty...great help!

 
It is very exciting searching the namespaces...
For the rename there are alot other ways. like:

Dim theRenamedFile As New System.IO.FileInfo("your_file_name")
theRenamedFile.MoveTo("...")

-or-

Microsoft.VisualBasic.FileSystem.Rename("..", "....")



eeetttccc ...
 
This works fine...but if the file is open it gives error message "An unhandled exception of type 'System.ArgumentException' occurred in microsoft.visualbasic.dll
Additional information: Procedure call or argument is not valid."

Is there a way, prior to renaming if we can close the file?

Thanks.
 
First add:

Try
' code to rename
Catch ex As Exception ' or System.ArgumentException
' MessageBox.Show("The file is already open. Close it!")
Finally
' code to execute whether there is exception or not.
End Try


I'll post again a little later.
 
Thanks for the tip.
My program can delete and rename but when the file is open it needs user input to actuly close by giving message. My requirements are if program finds the file open, the program closes it.
 
Has the user opened the file through your program or some other program?

[vampire][bat]
 
My question is I need to check with my program if file is opened (user might have opened the file by double clicking)program will close it then 2nd step will be rename or edit.
 
My point was that if the file was opened through your program you would be able to keep track of it - if external then another approach would be necessary.


[vampire][bat]
 
If my program opens a file, I guess it can close it. But what if users have opended it by double clicking it(External).
Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top