472,126 Members | 1,614 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,126 software developers and data experts.

Problem Checking If A File Exists In VB.NET

Hi,
I'm checking if a file exists with the following code;

Expand|Select|Wrap|Line Numbers
  1. If System.IO.File.Exists("C:\test.txt") = True Then
  2.      MsgBox("File Exists")
  3. Else
  4.       MsgBox("File Does Not Exist")
  5. End If
For some reason it always returns false even though the file does exist. Any suggestions? Could this be a security issue?
Jun 4 '07 #1
13 23589
Plater
7,872 Expert 4TB
The reason it always says false is that the file doesn't exist, know why? Because you have a \t in there, which means "put a tab character here".
It's an easy miss.
Do this:
Expand|Select|Wrap|Line Numbers
  1. If System.IO.File.Exists("C:\\test.txt") = True Then
  2. MsgBox("File Exists")
  3. Else
  4. MsgBox("File Does Not Exist")
  5. End If
  6.  
Jun 4 '07 #2
The reason it always says false is that the file doesn't exist, know why? Because you have a \t in there, which means "put a tab character here".
It's an easy miss.
Do this:
Expand|Select|Wrap|Line Numbers
  1. If System.IO.File.Exists("C:\\test.txt") = True Then
  2. MsgBox("File Exists")
  3. Else
  4. MsgBox("File Does Not Exist")
  5. End If
  6.  
Thanks for the reply,

but that unfortunately didn't resolve it. I don't think it's the code I think it's an Environment issue. I went back and tried the same code in VS.NET 2003 and it works. But it won't work in VS.NET 2005. Any other suggestions would be greatly appreciated.

Thanks
Jun 5 '07 #3
Plater
7,872 Expert 4TB
I think it is *your* specific environment. I use vs2005 and copy pasted that code and it worked correctly.
Told me file didn't exist, then I create the file and it told me it did exist.

Are you SURE the file actually exists?
Jun 5 '07 #4
lol...yeah the file exists. That was the first thing I checked for.
Jun 5 '07 #5
Plater
7,872 Expert 4TB
not sure what to tell ya then, works fine for me in vs2005.
Jun 5 '07 #6
Thanks anyways for your help. Now I'm running vs2003 and vs2005 on the same computer. Do you think that could be causing my issue?
Jun 5 '07 #7
Hey Mr D,

I've had problems with this all afternoon. It ended up being the naming of a file in XP. My code was looking for file.jpg but the file was named file.jpg.jpg. Double check you haven't got an extra extension.
Jun 25 '07 #8
dip_developer
648 Expert 512MB
Hi,
I'm checking if a file exists with the following code;

If System.IO.File.Exists("C:\test.txt") = True Then
MsgBox("File Exists")
Else
MsgBox("File Does Not Exist")
End If

For some reason it always returns false even though the file does exist. Any suggestions? Could this be a security issue?
try this.........

Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim filename as String="C:\test.txt" 
  3. Dim fFile As New FileInfo(filename)
  4. If Not fFile.Exists Then
  5. MsgBox("File Doesn't Exist")
  6. Else
  7. MsgBox("File Exists")
  8. End If
  9.  
Jun 26 '07 #9
My suggestion may come too late but anyway...

If you create a project and assign the project location to a network, you will probably get this message:

"The project location is not trusted:
Running the application may result in security exceptions when it attempts to perform actions which require full trust.
"

Check the project location where you save your project. Copy the project folder to a local c drive and it should work. Tested working using VS.NET 2005.

In short, the File.Exists code does not work if your project is saved to a network.
Dec 14 '09 #10
Frinavale
9,735 Expert Mod 8TB
I was just about to say, it's probably because your application is running under a Windows user account with permissions that don't let you access the C:\ directory.

-Frinny
Dec 15 '09 #11
Plater
7,872 Expert 4TB
If you don't have permision, a security exception should have been thrown
Dec 15 '09 #12
All,

I just had the same problem here on VS2005. It worked fine on my own PC and even worked from my PC when run from a network drive. I then tested it from another PC and it wouldn't work from the network drive. No exceptions thrown or anything. File.Exists just returned false for a file that existed. Directory.Exists did the same for a directory that existed.

I then copied the exe onto the required PC and it worked fine!!

Bit of a pain!! An hour of my life gone!! :)

T.
Aug 30 '10 #13
Plater
7,872 Expert 4TB
.NET apps running from a network drive run under a different set of "rules" (they have the "zones" management style like that of internet explorer)
There are some hoops you can jump through to get it to owkr (configure per computer, use clickOnce deployment) but it is probably easier to just run the app locally.
Aug 31 '10 #14

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by matt dittman | last post: by
13 posts views Thread by ashu | last post: by
15 posts views Thread by Geiregat Jonas | last post: by
2 posts views Thread by jcrouse | last post: by
9 posts views Thread by weidongtom | last post: by
2 posts views Thread by Bart | last post: by
20 posts views Thread by ongaro.admin | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.