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

Bugs with File.Exists()?

P: n/a
Hi Group,

I encountered something strange with File.Exists(string path). This
function is simply to check whether a file exists in certain path. However,
if the path starts with "file:\" in the begining of the string, no matter
the file exists or not. it will return false without any exception. It is
expected?

Regards,
Alex
Nov 16 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
I'm curious. What version of windows os allows you have "file" as a drive
letter?

"Alex Chan" <Ga********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Hi Group,

I encountered something strange with File.Exists(string path). This
function is simply to check whether a file exists in certain path. However, if the path starts with "file:\" in the begining of the string, no matter
the file exists or not. it will return false without any exception. It is
expected?

Regards,
Alex

Nov 16 '05 #2

P: n/a
Nope. for example, the path like that "file:\c:\temp\foo.txt" will return
false in every case, while "c:\temp\foo.txt" wont
"ESPN Lover" <es**@lover.com> wrote in message
news:eD**************@TK2MSFTNGP11.phx.gbl...
I'm curious. What version of windows os allows you have "file" as a drive
letter?

"Alex Chan" <Ga********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Hi Group,

I encountered something strange with File.Exists(string path). This
function is simply to check whether a file exists in certain path.

However,
if the path starts with "file:\" in the begining of the string, no matter the file exists or not. it will return false without any exception. It is expected?

Regards,
Alex


Nov 16 '05 #3

P: n/a
So then what's the issue? If you're checking for a malformed file path, you
should get false. Simply strip off the file:\ from your string

Try this:
if (p.IndexOf(@"file:\") == 0)

p = p.Substring(6);

"Alex Chan" <Ga********@hotmail.com> wrote in message
news:em**************@TK2MSFTNGP12.phx.gbl...
Nope. for example, the path like that "file:\c:\temp\foo.txt" will return
false in every case, while "c:\temp\foo.txt" wont
"ESPN Lover" <es**@lover.com> wrote in message
news:eD**************@TK2MSFTNGP11.phx.gbl...
I'm curious. What version of windows os allows you have "file" as a drive
letter?

"Alex Chan" <Ga********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Hi Group,

I encountered something strange with File.Exists(string path). This function is simply to check whether a file exists in certain path.

However,
if the path starts with "file:\" in the begining of the string, no

matter the file exists or not. it will return false without any exception. It is expected?

Regards,
Alex



Nov 16 '05 #4

P: n/a
Hi,

The reason why i felt that strange is the "file:\" is come with
Path.GetDirectoryName. My idea is to determine the location of the config
file storing in the current path of the executable through the following

szConfigFileName=
Path.GetDirectoryName(Assembly.GetExecutingAssembl y().CodeBase) +
"\\Config.xml";

For this, you will get the path starting with "file:\", and i check that
with File.Exists before opening the file, it always return false and none of
the settings got read.

Actually your suggestion is what i am using.

Regards,

Alex

"ESPN Lover" <es**@lover.com> wrote in message
news:uF**************@TK2MSFTNGP11.phx.gbl...
So then what's the issue? If you're checking for a malformed file path, you should get false. Simply strip off the file:\ from your string

Try this:
if (p.IndexOf(@"file:\") == 0)

p = p.Substring(6);

"Alex Chan" <Ga********@hotmail.com> wrote in message
news:em**************@TK2MSFTNGP12.phx.gbl...
Nope. for example, the path like that "file:\c:\temp\foo.txt" will return
false in every case, while "c:\temp\foo.txt" wont
"ESPN Lover" <es**@lover.com> wrote in message
news:eD**************@TK2MSFTNGP11.phx.gbl...
I'm curious. What version of windows os allows you have "file" as a drive letter?

"Alex Chan" <Ga********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
> Hi Group,
>
> I encountered something strange with File.Exists(string path). This > function is simply to check whether a file exists in certain path.
However,
> if the path starts with "file:\" in the begining of the string, no

matter
> the file exists or not. it will return false without any exception.

It is
> expected?
>
> Regards,
> Alex
>
>



Nov 16 '05 #5

P: n/a
Alex Chan wrote:
Hi,

The reason why i felt that strange is the "file:\" is come with
Path.GetDirectoryName. My idea is to determine the location of the config
file storing in the current path of the executable through the following

szConfigFileName=
Path.GetDirectoryName(Assembly.GetExecutingAssembl y().CodeBase) +
"\\Config.xml";

For this, you will get the path starting with "file:\", and i check that
with File.Exists before opening the file, it always return false and none of
the settings got read.

Actually your suggestion is what i am using.

Regards,

Alex

"ESPN Lover" <es**@lover.com> wrote in message
news:uF**************@TK2MSFTNGP11.phx.gbl...
So then what's the issue? If you're checking for a malformed file path,


you
should get false. Simply strip off the file:\ from your string

Try this:
if (p.IndexOf(@"file:\") == 0)

p = p.Substring(6);

"Alex Chan" <Ga********@hotmail.com> wrote in message
news:em**************@TK2MSFTNGP12.phx.gbl...
Nope. for example, the path like that "file:\c:\temp\foo.txt" will
return
false in every case, while "c:\temp\foo.txt" wont
"ESPN Lover" <es**@lover.com> wrote in message
news:eD**************@TK2MSFTNGP11.phx.gbl...

I'm curious. What version of windows os allows you have "file" as a


drive
letter?

"Alex Chan" <Ga********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl.. .

>Hi Group,
>
> I encountered something strange with File.Exists(string path).


This
>function is simply to check whether a file exists in certain path.

However,

>if the path starts with "file:\" in the begining of the string, no

matter

>the file exists or not. it will return false without any exception.
It
is

>expected?
>
> Regards,
>Alex
>
>



Sounds like you're using remoting.

System.IO.File.Exists works on the local machine only. It works the same
way as any other file on a local machine e.g. "c:\hello.txt"

You MUST be doing something using remoting otherwise it will return the
path on the local machine.

If not you are doing something so strange that I've yet to meet it.

Darwen.
Nov 16 '05 #6

P: n/a
"Alex Chan" <Ga********@hotmail.com> wrote in message
news:uX**************@TK2MSFTNGP10.phx.gbl...
szConfigFileName=
Path.GetDirectoryName(Assembly.GetExecutingAssembl y().CodeBase) +
"\\Config.xml";


I believe for this, yo'd want:

szConfigFileName=

Path.GetDirectoryName(Server.MapPath(Assembly.GetE xecutingAssembly().CodeBas
e)) +
"\\Config.xml";

"file://" is a URL protocol, like "http://" or "ftp://". File.Exist doesn't
understand it. MapPath converts a URL address to a local disk address.

--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
Nov 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.