Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

vb.net checking if file exists problem

Posted on 2011-03-03
4
Medium Priority
?
1,017 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 400 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 1600 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

597 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