473,769 Members | 7,810 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 24195
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 delivery, I need to be able to delete all the files in the drop directory. I can delete them via...
13
2783
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
114212
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
4549
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
2519
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 increase_arrays(), words's original value is modified, and if I tried to access it, I get <address 0x11 out of...
2
3476
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 the radiobuttonlist, so the Fileupload control looses the chosen file. I put the...
7
19117
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 way!
20
2172
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 don't open, are very slow, or even get crashed and corrupted. The weird is that even copying them...
4
14994
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"); System.out.println("THIS IS THE HOST: " + host); SshClient ssh = new SshClient();
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10219
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9998
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8876
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6675
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.