views:

8128

answers:

6

I am trying to write a function to determine if a file exists. The two methods prove to return inconsistent results (fileExists() seems to provide accurate results, compared to isFileFound(), that returns false positives - i would have expected an exception when trying to create the instance).

protected bool isFileFound(string path, string fileName)
    {
        System.IO.FileInfo fi = null;

        bool found = false;
        try
        {
            fi = new System.IO.FileInfo(path + fileName);
            found = true;
        }
        catch (Exception e)
        {
            baselogger.Fatal(e.Message + " " + e.StackTrace + " \n" + path + fileName);
        }

        return found;
    }

    protected bool fileExists(string path, string pattern)
    {
        bool success = false;

        try
        {
            success = File.Exists(path + pattern);
        }
        catch (Exception e)
        {
            baselogger.Warn(e.Message + " " + e.StackTrace + " " + e.Source);
        }

        return success;
    }

Neither seems to be able to resolve a UNC path of the following syntax: \\abcserver\c$\xyzfolder\foo.bar

Any idea why the unc path is failing for these methods would be greatly appreciated.

+9  A: 

You can create a FileInfo for an non-existing file. But then you can check the FileInfo.Exists property to determine whether the file exists, e.g:

FileInfo fi = new FileInfo(somePath);
bool exists = fi.Exists;

Update: In a short test this also worked for UNC paths, e.g. like this:

FileInfo fi = new FileInfo(@"\\server\share\file.txt");
bool exists = fi.Exists;

Are you sure that the account (under which your application is running) has access to the share. I think that (by default) administrative rights are required to access the share "c$".

M4N
Ok, but what about the UNC path? Even with the modification to this method, neither method can resolve the UNC path format in my original question.
steve_mtl
Tried an alternate folder witha d$ share, and it worked. A little digging concluded that the UNC path is valid, but some permissions changes are needed.
steve_mtl
+2  A: 

See this question:
http://stackoverflow.com/questions/265953/c-how-can-you-easily-check-if-access-is-denied-for-a-file

The short version of that question is that you don't, because the file system is volatile. Just try to open the file and catch the exception if it fails.

The reason your isFileFound method doesn't work is because the FileInfo structure you are using can also be used to create files. You can create a FileInfo object with the desired info for a non-existing file, call it's .Create() method, and you've set your desired properties all at once.

I suspect the reason the UNC path fails is either 1) a permissions issue access the admin share from the user running your app, or 2) The $ symbol is throwing the method off, either because it's not being input correctly or because of a bug in the underlying .Exists() implementation.

Joel Coehoorn
Beware that catching thrown exceptions can be very expensive. If you are going to be performing batch file operations you would be much better of testing for file existence *and* catching exceptions.
Ifeanyi Echeruo
+2  A: 

This may or may not be the case, but could you be joining your path an file name incorrectly for one of your cases.

This:

success = File.Exists(path + pattern);

vs:

success = File.Exists(Path.Join(path,pattern));

llamaoo7
A: 

This can help you:
http://www.codeplex.com/FileDirectoryPath
It's NDepend.Helpers.FilePathDirectory, that have a "Path validity check API" among other that can be useful.

Click Ok
+1  A: 

So i went with the

bool success = File.Exists(path + Filename);

option, as opposed to using the FileInfo route.

Thanks for all the suggestions!

steve_mtl
You may want to use System.IO.Path.Combine(path, Filename) instead of string addition.This takes care of corner cases e.g. "a\" + "b" = "a\b" and Path.Combine("a\", "b") = "a\b"but"a" + "b" = "ab" and Path.Combine("a", "b") = "a\b"
Ifeanyi Echeruo