Solved

vb.net checking if file exists problem

Posted on 2011-03-03
4
1,003 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
[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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
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 …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

717 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