473,480 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 14371

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
5072
by: Leslaw Bieniasz | last post by:
Cracow, 3.01.2005 Hello, When opening a file stream in the append mode, either a new file is created (if a specified file does not exist), or an existing file is opened for adding stuff. Is...
14
7674
by: Matt | last post by:
Hello, I see other references in this newsgroup saying that the only standard C++ way to test for file existence is some variant of my code below; can someone please confirm...or offer...
13
2275
by: Joseph Oget | last post by:
If I use the code below in a VB.NET form, even if the file exists, and is then executed, the Else statement produces the "Sorry cannot find the File " error message. The MessageBox.Show(...
3
4933
by: Jim Hunter | last post by:
From what I've read, the only indication fprintf gives of an error is a negative return value. I have a series of writes to a file using fprintf, and, while I need to know if a failure has...
4
1434
by: Tony Cheng | last post by:
I want to know which account will be used when File.Exists are called in ASP.NET ? Thx
3
323
by: Mike | last post by:
Thanks for the reply, I have been trying that, but I keep getting the same results. The result I get is that the file exits, when it really doesn't. All my msgbox display twice and I'm not sure...
26
4912
by: Army1987 | last post by:
Is this a good way to check wheter a file already exists? #include <stdio.h> #include <stdlib.h> int ask(const char *prompt); typedef char filename; int main(int argc, char *argv) { FILE...
7
19009
by: sprash | last post by:
Newbie question: I'm trying to determine if a file physically exists regardless of the permissions on it Using File.Exists() returns false if it physically exists but the process does not...
2
2691
by: Visine_Eyes | last post by:
What .NET mechanism should I use to resolve permission issues before I begin to copy directory structure/file(s) from one computer to another. I have tried Try/Catch statement using DirectoryInfo...
0
7037
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6904
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7076
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6730
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5321
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4767
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
2976
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1294
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
174
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.