To my surprise, I am finding out that what the MSDN documentation is not true.
"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."
Following are the results of some tests:
using System;
using System.IO;
namespace console.playground
{
class DoesFileExist
{
[STAThread]
static void Main(string[] args)
{
// check and see if a file exists on local drive, run as user has rights
// > returns true for both
//string path = "c:\\temp\\procedures.prg";
// check and see if a file exists on local drive, run as user does not
have rights
// > returns true for both. surprise since msdn indicates it will return
false
//string path = "c:\\temp\\admin.txt";
// check and see if a file exists on network drive, run as user has rights
// > returns true for both
//string path = "k:\\boot.ini";
// check and see if a file exists on network drive, run as user does not
have rights
// > returns true for both. surprise since msdn indicates it will return
false
//string path = "f:\\test.txt";
string path = "f:\\filedoesnotexist.txt"; // this returns false
FileInfo fi = new FileInfo(path);
bool fe = fi.Exists;
if (!fe)
{
Console.WriteLine("File Does NOT Exist");
}
else
{
Console.WriteLine("File Does Exist");
}
if (! File.Exists(path))
{
Console.WriteLine("File Does NOT Exist");
}
else
{
Console.WriteLine("File Does Exist");
}
Console.ReadLine();
}
}
}
"Chris Fink" wrote:
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."
So this explains my problem. But in my code, I would like to know if 1)the
file does not exist or 2)the file does exist, but the user does not have
sufficient permissions. So my question is, how do I determine (catch) if the
problem is insufficient permissions.
Is there another class that I can use or am I simply out of luck?