469,319 Members | 2,408 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,319 developers. It's quick & easy.

How to use File.Exists and Handle Insufficient Permissions

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
2 13683

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

Similar topics

14 posts views Thread by Leslaw Bieniasz | last post: by
13 posts views Thread by Joseph Oget | last post: by
4 posts views Thread by Tony Cheng | last post: by
3 posts views Thread by Mike | last post: by
26 posts views Thread by Army1987 | last post: by
2 posts views Thread by Visine_Eyes | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.