473,569 Members | 2,791 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem Checking If A File Exists In VB.NET

4 New Member
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 24164
Plater
7,872 Recognized Expert Expert
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
darkslide
4 New Member
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 Recognized Expert Expert
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
darkslide
4 New Member
lol...yeah the file exists. That was the first thing I checked for.
Jun 5 '07 #5
Plater
7,872 Recognized Expert Expert
not sure what to tell ya then, works fine for me in vs2005.
Jun 5 '07 #6
darkslide
4 New Member
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
sjcrichton
1 New Member
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 Recognized Expert Contributor
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
TasChew
1 New Member
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

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

Similar topics

5
1113
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 until the CDO.DropDirectory.GetMessages() method is called. At this point, the files are locked until I shut down the service. After processing and...
13
2764
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 provides simple interest & withdraw facilities but no check book facilities. the current account provides check book facilities but no interest.
15
114187
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
4539
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 System.EventArgs) Handles Button1.Click Dim strInput As String = Application.StartupPath & "\CreateMameGamesCFG.bat" If lblMameExePath.Text <> "" Then
9
2490
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 *after* calling increase_arrays(), I received segmentation fault. I tried to step it through gdb, and I found out that after calling...
2
3473
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 'no'. I get no error, but if i take 'yes', the new file which overwrites the existing one is empty. I think it's because of the postabck caused by...
7
19050
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 have the necessary permissions. One hack could be to check for length and that would throw a FileNotFoundException ...but there is got to be a better...
20
2122
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 software. The hardware has been tested several times. Viruses, trojans and so on: clean for sure. The problem is that .mdb files (_only .mdb files_) often...
4
14894
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 it: this is the code that I am using that is not working properly: System.out.println("YOU ARE IN UPLOAD"); ...
0
7605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7917
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7665
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6277
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5501
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2105
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.