473,796 Members | 2,704 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(oF ile);
}

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 2622
What do you get when you run " Directory.GetCu rrentDirectory( )"? Is
the result the same path as where your "app.cfg" file is located at?
On Jun 23, 1:19*pm, Anthony Papillion <papill...@gmai l.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(oF ile);

}

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...@ma ilinator.com wrote:
What do you get when you run " Directory.GetCu rrentDirectory( )"? Is
the result the same path as where your "app.cfg" file is located at?
OK when I ran Directory.GetCu rrentDirectory( ) 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:\Documen ts
and Settings\Anthon y\My Documents\Visua l Studio 2008\Projects\M yProject
\Debug". Both the file not found error and the GetCurrentDirec tory()
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*******@gmai l.comwrote in message
news:ad******** *************** ***********@8g2 000hse.googlegr oups.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(oF ile);
}

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.c omwrote:
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...@gmai l.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...@gmai l.comwrote:
On Jun 23, 7:50 pm, "Jeff Winn" <jw...@nospam.c omwrote:
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...@gmai l.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...@gmai l.comwrote:
Hello Everyone,
<snap>
if(File.Exist(" app.cfg")){
FileStream oFile = new FileStream("app .cfg", FileMode.Open,
FileAccess.Read );
StreamReader oReader = new StreamReader(oF ile);

}
<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
2482
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 don't remember any good reasons (not that I haven't read them, I'm sure, but it's more of a failure to hold them in my brain). Here is an attempt that I was commissioned to write, and I'd appreciate any comments and/or criticism (specific or...
2
14410
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 states, "If the caller does not have sufficient permissions to read the specified file, no exception is thrown and the method returns false regardless of the existence of path."
4
4084
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 or at least file name . And if does not exist Fasle or empty string. Of course I can use VB6 function Dir, but maybe .Net contains
1
2592
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? My code is creating a file correctly, but the CreationTime is (sometimes) set to the CreationTime of an old file that has previously been removed. The line of code creating the file is _LogFileWriter = newFile.CreateText()
52
7545
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 file doesn't exist.Now tell me what should I do! Thanks
26
4960
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 *in, *out;
7
19121
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!
3
2054
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"; if(!chmod($path, 0744)) { echo "error, $path"; exit; } else {
65
5107
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 got errors of Failed to open file ./outdir/mytestout.txt. Below is the code: #include <stdio.h>
0
9535
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
10467
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...
0
10244
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10201
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
9061
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
6802
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();...
1
4130
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
2
3744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2931
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.