473,387 Members | 1,590 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,387 software developers and data experts.

File.Exist() misreporting?

Hello Everyone,

I'm writing some code that needs to check if a file exists or not. I'm
using the System.IO's File.Exist() method and it's either misreporting
or I am not fully understanding how this class works. Here's what I am
doing:

if(File.Exist("app.cfg")){
FileStream oFile = new FileStream("app.cfg", FileMode.Open,
FileAccess.Read);
StreamReader oReader = new StreamReader(oFile);
}

Simple enoiugh, right? Basically, if the file exists, create a new
FileStream and attach a StreamReader to it. But, for some reason, even
though the file DOES NOT exist, the code is still validating the
File.Exist() method and trying to open the file.

Is this code wrong? Could I be experiencing a problem with the
compiler? Can someone give a guy a clue? lol

Thanks in advance,
Anthony
Jun 27 '08 #1
8 2604
What do you get when you run " Directory.GetCurrentDirectory()"? Is
the result the same path as where your "app.cfg" file is located at?
On Jun 23, 1:19*pm, Anthony Papillion <papill...@gmail.comwrote:
Hello Everyone,

I'm writing some code that needs to check if a file exists or not. I'm
using the System.IO's File.Exist() method and it's either misreporting
or I am not fully understanding how this class works. Here's what I am
doing:

if(File.Exist("app.cfg")){
* * FileStream oFile = new FileStream("app.cfg", FileMode.Open,
FileAccess.Read);
* * StreamReader oReader = new StreamReader(oFile);

}

Simple enoiugh, right? Basically, if the file exists, create a new
FileStream and attach a StreamReader to it. But, for some reason, even
though the file DOES NOT exist, the code is still validating the
File.Exist() method and trying to open the file.

Is this code wrong? Could I be experiencing a problem with the
compiler? Can someone give a guy a clue? lol

Thanks in advance,
Anthony
Jun 27 '08 #2
On Jun 23, 1:42 pm, qglyirnyf...@mailinator.com wrote:
What do you get when you run " Directory.GetCurrentDirectory()"? Is
the result the same path as where your "app.cfg" file is located at?
OK when I ran Directory.GetCurrentDirectory() it reported that the
current directory was the same directory that the error was occurring
in. I don't remember the exact path but let's say it's "C:\Documents
and Settings\Anthony\My Documents\Visual Studio 2008\Projects\MyProject
\Debug". Both the file not found error and the GetCurrentDirectory()
function report the same directory.

Here's the thing though; I don't expect the file to be there on the
programs first run. That's why I have the check in their. And I have
an else{} block right after the if{} block I printed above to handle
the non-existence of the file. What seems to be happening though is
that the File.Exists() method simply isn't reporting false as it
should when the file isn't there.

Any clue?

Thanks!
Anthony
Jun 27 '08 #3
Do you have the Visual Studio hosting process enabled for the project you're
trying to debug? I've seen it do a lot of strange things when it's enabled
so I usually just turn it off. I don't use edit-and-continue anyway, so I
have no real use for it.

My guess, that's what is causing your problem if it's enabled. To check
right click your project (not the solution) and go to properties from the
drop down. Go to the debug tab and at the bottom you should see a checkbox
indicating whether it's enabled or not.

Clean the solution and rebuild it, that usually fixes any synchronization
issues I have.

"Anthony Papillion" <pa*******@gmail.comwrote in message
news:ad**********************************@8g2000hs e.googlegroups.com...
Hello Everyone,

I'm writing some code that needs to check if a file exists or not. I'm
using the System.IO's File.Exist() method and it's either misreporting
or I am not fully understanding how this class works. Here's what I am
doing:

if(File.Exist("app.cfg")){
FileStream oFile = new FileStream("app.cfg", FileMode.Open,
FileAccess.Read);
StreamReader oReader = new StreamReader(oFile);
}

Simple enoiugh, right? Basically, if the file exists, create a new
FileStream and attach a StreamReader to it. But, for some reason, even
though the file DOES NOT exist, the code is still validating the
File.Exist() method and trying to open the file.

Is this code wrong? Could I be experiencing a problem with the
compiler? Can someone give a guy a clue? lol

Thanks in advance,
Anthony
Jun 27 '08 #4
On Jun 23, 7:50*pm, "Jeff Winn" <jw...@nospam.comwrote:
Do you have the Visual Studio hosting process enabled for the project you're
trying to debug? I've seen it do a lot of strange things when it's enabled
so I usually just turn it off. I don't use edit-and-continue anyway, so I
have no real use for it.

My guess, that's what is causing your problem if it's enabled. To check
right click your project (not the solution) and go to properties from the
drop down. Go to the debug tab and at the bottom you should see a checkbox
indicating whether it's enabled or not.

Clean the solution and rebuild it, that usually fixes any synchronization
issues I have.
Well that was a good guess and hosting process was indeed enabled. I
went in and disabled (unchecked) it and rebuilt the solution.
Unfortunately, that didn't do anything at all. Still totally skipping
the File.Exist() statement. Weird...

Thank You!
Anthony
Jun 27 '08 #5
On Jun 23, 11:09*pm, Anthony Papillion <papill...@gmail.comwrote:

<snip>
Here's the thing though; I don't expect the file to be there on the
programs first run. That's why I have the check in their. And I have
an else{} block right after the if{} block I printed above to handle
the non-existence of the file. What seems to be happening though is
that the File.Exists() method simply isn't reporting false as it
should when the file isn't there.
Could you post a short but complete program which demonstrates the
problem?

See http://pobox.com/~skeet/csharp/complete.html for details of what I
mean by that.

Jon
Jun 27 '08 #6
On Jun 24, 1:02 am, Anthony Papillion <papill...@gmail.comwrote:
On Jun 23, 7:50 pm, "Jeff Winn" <jw...@nospam.comwrote:
Do you have the Visual Studio hosting process enabled for the project you're
trying to debug? I've seen it do a lot of strange things when it's enabled
so I usually just turn it off. I don't use edit-and-continue anyway, so I
have no real use for it.
My guess, that's what is causing your problem if it's enabled. To check
right click your project (not the solution) and go to properties from the
drop down. Go to the debug tab and at the bottom you should see a checkbox
indicating whether it's enabled or not.
Clean the solution and rebuild it, that usually fixes any synchronization
issues I have.

Well that was a good guess and hosting process was indeed enabled. I
went in and disabled (unchecked) it and rebuilt the solution.
Unfortunately, that didn't do anything at all. Still totally skipping
the File.Exist() statement. Weird...

Thank You!
Anthony
Is the file in question part of your solution? And if so, when you
click on it, what is its build action set to?

Chris
Jun 27 '08 #7
On Jun 23, 7:19 pm, Anthony Papillion <papill...@gmail.comwrote:
Hello Everyone,

I'm writing some code that needs to check if a file exists or not. I'm
using the System.IO's File.Exist() method and it's either misreporting
or I am not fully understanding how this class works. Here's what I am
doing:

if(File.Exist("app.cfg")){
<snap>
Error 1 'System.IO.File' does not contain a definition for 'Exist'

Jun 27 '08 #8
On Jun 23, 7:19 pm, Anthony Papillion <papill...@gmail.comwrote:
Hello Everyone,
<snap>
if(File.Exist("app.cfg")){
FileStream oFile = new FileStream("app.cfg", FileMode.Open,
FileAccess.Read);
StreamReader oReader = new StreamReader(oFile);

}
<snap>
How and what are you actually running. because you can not run the
above code...
Jun 27 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Andrew Clark | last post by:
*** post for FREE via your newsreader at post.newsfeed.com *** Hello all, I recall several threads over the years about how reading file size cannot be done consistantly or portably, but I...
2
by: Chris Fink | last post by:
I am using the System.IO.File class to determine if a file exists on a network share. The File.Exists method keeps returning false, even though the file does exist. The MSDN documentation...
4
by: Mike | last post by:
Hi, I am looking for function in .Net library that let me know if exist any file if I specified template. Eg: I specify "*.txt" and if any file (1.txt, 2.txt, .. ) exists then I can get True...
1
by: Tim Failes | last post by:
This seems a trival question, but I cannot get it to work properly... Essentially my question is, how can I create a text file, and guarantee it is given the current date/time as the Creation Time?...
52
by: paytam | last post by:
Hi all Can anyone tell me how can I check that a file exist or no.I mean when you use this commands FILE *fp; if(!fp) //Could not open the file doen't show why it can not open it,may be the...
26
by: Army1987 | last post by:
Is this a good way to check wheter a file already exists? #include <stdio.h> #include <stdlib.h> int ask(const char *prompt); typedef char filename; int main(int argc, char *argv) { FILE...
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...
3
by: brook | last post by:
hey all - i´m new to php and having trouble writing a simple code which should create a file. here is the most simplified version: <?php $content = "my content"; $path = "test.txt";...
65
by: Hongyu | last post by:
Dear all: I am trying to write to a file with full directory name and file name specified (./outdir/mytestout.txt where . is the current directory) in C programming language and under Unix, but...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
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,...
0
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...

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.