By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,947 Members | 1,594 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,947 IT Pros & Developers. It's quick & easy.

How to use File.Exists and Handle Insufficient Permissions

P: n/a
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?
Nov 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Chris Fink" <Ch*******@discussions.microsoft.com> wrote in message
news:EE**********************************@microsof t.com...
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.
Nov 17 '05 #2

P: n/a
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?

Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.