-
April 5th, 2006, 09:20 AM
#1
Problem with IO.File.Exists Method
Hi Gurus,
I am having troubles with IO.File.Exists, occasionally the File.Exists method returns false when the file actually does exist. If I shutdown the application and run it again it suddenly recognises that the file does exist and then continues ok. I have seen this in both debug and release builds.
I know Java has a problem with stale file handles on it's IO.File.Exists, does anyone know if this is the same for .NET? Anyone know of this problem and an effective work around, as it is a not very nice behavior to try and explain to users?
Thanks in advance.
KnNeeded
-
April 5th, 2006, 09:26 AM
#2
Re: Problem with IO.File.Exists Method
Are you trying to check the File which is present on a Network drive?
Are you doing this in ASP.NET?
-
April 5th, 2006, 09:29 AM
#3
Re: Problem with IO.File.Exists Method
Can you post some snippets of code where you use it? I use the method several times and got no error until now.
Useful or not? Rate my posting. Thanks.
-
April 5th, 2006, 11:46 AM
#4
Re: Problem with IO.File.Exists Method
I use FileInfo and don't have any problems.
Code:
string strPath = "C:\\file.txt";
FileInfo fi = new FileInfo(strPath);
if (fi.Exists)
{
// do whatever
}
Last edited by Tron; April 5th, 2006 at 12:53 PM.
-
April 5th, 2006, 12:09 PM
#5
Re: Problem with IO.File.Exists Method
internally both File.Exists(...) and FileInfo.Exists obtain the "exists" info the same way (they check the file's attributes for the Directory bit).
-
April 6th, 2006, 07:46 PM
#6
Re: Problem with IO.File.Exists Method
Hi Gurus,
Thanks for the replys, here is some answers to the questions posed:
Shuja Ali wrote:
Are you trying to check the File which is present on a Network drive?
Are you doing this in ASP.NET?
- No I am trying to find a file in the same location as the executable.
- It is a C# Windows Dialog Application not ASP.NET.
Here is a code snippet:
Code:
if (!File.Exists("ProductRegister.csv"))
return null;
The only thing I can think of is the file name in some cases does not seach in the executables local directory, however I can't understand why I get inconsistant behavior?
Note: I am using the static function File.Exists and not creating an object such as a FileInfo object to use the function, I shouldn't need to for a static function.
Any other ideas?
Thanks in Advance
KnNeeded
-
April 6th, 2006, 08:04 PM
#7
Re: Problem with IO.File.Exists Method
you need to specify the full path to the file. by default it uses (i think) the local dir that the executable is in. you can set the environment working directory and it may not change by the time you call that function (as you dont have exclusive access to the working directory env variable).
your best bet will be to give the full path to the file (even if its in your local directory).
-
April 6th, 2006, 08:59 PM
#8
Re: Problem with IO.File.Exists Method
Originally Posted by KnNeeded
Hi Gurus,
Thanks for the replys, here is some answers to the questions posed:
- No I am trying to find a file in the same location as the executable.
- It is a C# Windows Dialog Application not ASP.NET.
Here is a code snippet:
Code:
if (!File.Exists("ProductRegister.csv"))
return null;
The only thing I can think of is the file name in some cases does not seach in the executables local directory, however I can't understand why I get inconsistant behavior?
Note: I am using the static function File.Exists and not creating an object such as a FileInfo object to use the function, I shouldn't need to for a static function.
Any other ideas?
Thanks in Advance
KnNeeded
You say that File.Exist fails every once in awhile, but are you repeating the exact same events?
Are you doing anything that could possibly change the current working directory? Relative pathnames are based from the CurrentWorking Directory and certain classes that you may be using will change it.
For instance, by default the OpenFileDialog changes the Current Working directory while you're navigating through other folders. There is a property that you can set called "RestoreDirectory" that you can set to true, so that it won't alter the CurrentDirectory after it's closed.
Jason Isom
.NET 2.0 / VS2005 Developer
-
April 7th, 2006, 10:00 PM
#9
Re: Problem with IO.File.Exists Method
Hi All,
Thanks Jason Isom that seems to be the answer it only happens after I have called one of the FileDialog Controls. I need to ensure I am getting the working directory of the executable, I will search of a way of getting that information in code, otherwise I will have to add a registry entry as a install procedure. I don't want to rely on the RestoreDirectory property because of the fact that other classes may alter the working directory as well.
Thanks for the help.
KnNeeded
-
April 7th, 2006, 11:54 PM
#10
Re: Problem with IO.File.Exists Method
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) will get your exe's path everytime.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|