473,378 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 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 24113
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

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

Similar topics

5
by: matt dittman | last post by:
I have created a windows service that reads emails from a drop directory and moves them to the appropriate mail folder every 15 seconds. I can move, rename and delete the files as needed, up...
13
by: ashu | last post by:
hi to all, Please read the following ques. first. assume that a bank maintains 2 kinds of accounts for customers one called as savings accounts & other as current account. the savings account...
15
by: Geiregat Jonas | last post by:
is using if(open("file",O_EXCL) != -1){ printf("File does exists")}else{printf("file does not exists"); } a good way of checking if a file exists or not, if not how should I do it ?
2
by: jcrouse | last post by:
I apologize for starting another thread but the old one had a weird subject line. Anyways...here is the code: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As...
9
by: weidongtom | last post by:
Hi, I've written the code that follows, and I use the function add_word(), it seems to work fine *before* increase_arrays() is called that uses realloc() to allocate more memory to words. But...
2
by: Bart | last post by:
Hi, i use the FileUpload control but if the uploaded file already exists, i want let the choice between overwriting or not the existing file. Therefore i use a radiobuttonlist with 'yes' and...
7
by: sprash | last post by:
Newbie question: I'm trying to determine if a file physically exists regardless of the permissions on it Using File.Exists() returns false if it physically exists but the process does not...
20
by: ongaro.admin | last post by:
Hi, I'm experiencing a strange problem with .mdb files. We have two buildings connected by optical fiber (a single LAN). Everything works perfect with any file, any size, any application...
4
by: ndedhia1 | last post by:
Hi. I am writing a java program in which I want to ftp a file to another unix box. First I have to check if the directory exists in which I am ftping into and if it does not exist, I have to create...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.