CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Dec 2001
    Posts
    199

    Unhappy 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

  2. #2
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    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?

  3. #3
    Join Date
    May 2003
    Location
    Germany
    Posts
    936

    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.

  4. #4
    Join Date
    May 2002
    Posts
    511

    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.

  5. #5
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

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

  6. #6
    Join Date
    Dec 2001
    Posts
    199

    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

  7. #7
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

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

  8. #8
    Join Date
    May 2004
    Location
    Greenville, NC
    Posts
    193

    Re: Problem with IO.File.Exists Method

    Quote 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

  9. #9
    Join Date
    Dec 2001
    Posts
    199

    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

  10. #10
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    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
  •  





Click Here to Expand Forum to Full Width

Featured