Solved

Checking for the existence of a File

Posted on 2006-11-21
4
171 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 86

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 86

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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 process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

710 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