Bugs with File.Exists()?

A

Alex Chan

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
 
E

ESPN Lover

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

Alex Chan

Nope. for example, the path like that "file:\c:\temp\foo.txt" will return
false in every case, while "c:\temp\foo.txt" wont
 
E

ESPN Lover

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);
 
A

Alex Chan

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.GetExecutingAssembly().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
 
D

David McClarnon

Alex said:
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.GetExecutingAssembly().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

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

James Curran

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

I believe for this, yo'd want:

szConfigFileName=

Path.GetDirectoryName(Server.MapPath(Assembly.GetExecutingAssembly().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!)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top