File.Exist() misreporting?

A

Anthony Papillion

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
 
Q

qglyirnyfgfo

What do you get when you run " Directory.GetCurrentDirectory()"? Is
the result the same path as where your "app.cfg" file is located at?
 
A

Anthony Papillion

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
 
J

Jeff Winn

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.
 
A

Anthony Papillion

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
 
J

Jon Skeet [C# MVP]

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
 
C

Chris Dunaway

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
 
G

GArlington

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'
 
G

GArlington

Hello Everyone,
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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top