How to use File.Exists and Handle Insufficient Permissions

G

Guest

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?
 
W

Willy Denoyette [MVP]

Chris Fink said:
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?

Just try to open the file, if it succeeds the file exists and is accesible,
if it fails, then the exception message will have enough info for you to
make the distinction between UnauthorizedAccess and FileNotFound.

Willy.
 
G

Guest

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top