Welcome to WebmasterWorld Guest from 54.224.103.186

Forum Moderators: ocean10000

Message Too Old, No Replies

why is mapPath necessary?

     
8:58 pm on Mar 14, 2006 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 1, 2004
posts:130
votes: 0


why will this work:


Filepath = Server.MapPath("readme.txt");
fs.FileExists(Filepath)...

but not this:


fs.FileExists("readme.txt")

1:44 pm on Mar 15, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 2, 2003
posts:1184
votes: 0


Because the FileExists() method needs to know the location of the file you'd like to determine existance on.
5:39 pm on Mar 15, 2006 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 1, 2004
posts:130
votes: 0


Isn't that what paths are for:

../here/here/file

is a location

and:

file.ext

is file in the current directory. Unles asp doesn't like relative paths... I don't get it.

5:46 pm on Mar 15, 2006 (gmt 0)

Preferred Member

10+ Year Member

joined:Dec 3, 2004
posts:445
votes: 0


Like EasyCoder stated, fs.FileExists needs a full path for which to find the file. ie

fs.FileExists("readme.txt") will not work,

fs.FileExists("../yourdir/readme.txt") will not work,

But this will.
fs.FileExists("C:\inetpub\wwwroot\yourdir\readme.txt")

So when you use Sever.MapPath you are literally using the easy way to providing a full path to fs.FileExists.

Use this to get the full path as the server sees it:
response.write Server.MapPath("readme.txt")

12:54 am on Mar 16, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 2, 2003
posts:1184
votes: 0


Keep in mind that the File System Object is executing somewhere from the System32 or another system folder, not your website folder. So the FileExists method needs to take that into account.

If the method worked the way you've asked here's what would happen... (psuedo code)

public bool FileExists(string in_relativeFileName)
{
// determine if in_relativeFileName lives in system32
return (bool) if exists relativeFileName in system32? true : false;
}

versus...
public bool FileExists(string in_absoluteFileName)
{
// determine if in_absoluteFileName lives specified location
return (bool) if exists absoluteFileName in specified location? true : false;
}