Solved

vb.net checking if file exists problem

Posted on 2011-03-03
4
963 Views
Last Modified: 2012-05-11
The following code in a windows service alway returns false:

        Dim f As New IO.FileInfo(sourceFolder)

        If f.Exists = 1 Then
            LogError("FILE EXISTS " & f.FullName)
        Else
            LogError("FILE DOES NOT EXIST " & f.FullName)
        End If

This is the value of f - - C:\FileExchange\Appointments\Pending


And it contains the file -- Text Document.txt

I won't always know the name of the text file, and there may be 1 or more. I need to check that there are files in the folder before I process them.

What wrong?

Andy
0
Comment
Question by:Andy Green
  • 2
4 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35024933
It's correct in returning false. You are passing the path of a directory and asking if it exists as a file, which it doesn't.

If you want to check for the existence of a file then you need to pass the path AND name of the file.
0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 100 total points
ID: 35024943
If you want to check for existence of the directory, and to see if it contains any *.txt files you would use:
Dim di As New DirectoryInfo(sourceFolder)
If di.Exists = True Then
     If di.GetFiles("*.txt").Count > 0 Then
            '// folder exists and has files in it
     End If
End If

Open in new window

0
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 400 total points
ID: 35024953
Dim f As New IO.DirectoryInfo(sourceFolder)
       
        If f.GetFiles().Length > 0 Then
          LogError("FILE EXISTS " & f.FullName)
        Else
            LogError("FILE DOES NOT EXIST " & f.FullName)
        End If
0
 
LVL 3

Author Closing Comment

by:Andy Green
ID: 35025015
Thanks All

Andy
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 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

12 Experts available now in Live!

Get 1:1 Help Now