Solved

Checking for the existence of a File

Posted on 2006-11-21
4
167 Views
Last Modified: 2010-04-30
Hi, I am using Visual Basic 2005.  I am trying to check for the existence of a file.  Apparently, I am not importing the correct libraries because FileExists is not recognized.  Any suggestions of syntax would be appreciated.  
0
Comment
Question by:VBStudent
  • 2
4 Comments
 
LVL 11

Expert Comment

by:ch2
ID: 17990651
Hi
 Try this

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Path As String
        Try
            Path = "yourfilepath"
            If IO.File.Exists(Path) Then
                MsgBox("File exists")
            Else
                MsgBox("File dosen't exists")
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

ch2
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 17990672
For VB.Net 2005, use the new "My" namespace:

        If My.Computer.FileSystem.FileExists("c:\someFile.txt") Then

        Else

        End If
0
 

Author Comment

by:VBStudent
ID: 17990737
That was a slick solution. Thank you for your quick response
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 17990805
Um...thanx.

Though it really isn't any better/worse than ch2's submission.

It just uses a different namespace...

There are LOTS of goodies in the "My" namespace though.

My.Application:
http://msdn2.microsoft.com/en-us/library/ms172938.aspx

My.Application.OpenForms:
http://msdn2.microsoft.com/en-us/library/eh13dca9.aspx

My.Computer:
http://msdn2.microsoft.com/en-us/library/ms172948.aspx

and lots of other stuff...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now