473,406 Members | 2,345 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 14366

"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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
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...
0
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.