FileExists in VS 2005 VB always returns false

I'm having issues with a simple FileExist check in old VS 2005 VB project that I need to make som alterations to.

The check for a file always returs false  - not matter if I use

Dir("c:\")
or
My.Computer.FileSystem.FileExists("c:\")

Any help would be much appreciated
LVL 1
Mik MakConsultantAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lokesh B RDeveloperCommented:
Hi,

I think you are checking for the drive not the file.

try this

This example checks to see whether or not the file Check.txt exists and supplies the information in a message box.

If My.Computer.FileSystem.FileExists("c://Check.txt") Then
    MsgBox("File found.")
Else
    MsgBox("File not found.")
End If

Open in new window


https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.filesystem.fileexists(v=vs.110).aspx
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mik MakConsultantAuthor Commented:
I think you're right :) - but what if I need to check in a subfolder - do I then also use double-forward slash ?
0
Saurabh Singh TeotiaCommented:
Use this way... And i'm assuming you want to check for abc.xlsx file

dim filename as string,xpath as string

xpath="C:\"
Filename="abc.xlsx"

If Len(Dir(xpath &  filename)) = 0 Then
  MsgBox "File does not exist"
else
'your code here
end if

Open in new window


Saurabh...
0
Mik MakConsultantAuthor Commented:
Hmm, now it also Work with backslash - I'm confused :)
0
Saurabh Singh TeotiaCommented:
Bojerene,

In order it to work you need to define the file name in it which was missing once you define the file name and check for len that way you can trap whether file is their or not...

Saurabh...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.