Solved

vb.net checking if file exists problem

Posted on 2011-03-03
4
998 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

Suggested Solutions

Title # Comments Views Activity
asp.net mvc5 6 32
Whice version VisualStudio for new Dev Laptop? 10 40
Make borderless form movable by user 2 25
VB .net 2010 Byte array 2 19
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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

737 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