File.Exists?

A

Arpan

A file named Form1.asp exists in the folder C:\Inetpub. The following
code exists in a file named FindFile.aspx which exists in
C:\Inetpub\wwwroot\ASPX:

<script runat="server">
Sub Page_Load(obj As Object, ea As EventArgs)
Dim ioFileInfo As FileInfo

ioFileInfo = New FileInfo(Server.MapPath("../../Form1.asp"))
If (ioFileInfo.Exists) Then
Response.Write("File Exists")
Else
Response.Write("File Doesnot Exist")
End If
End Sub
</script>

Now since FindFile.aspx exists in the folder C:\Inetpub\wwwroot\ASPX &
the file I am searching for (Form1.asp) exists in C:\Inetpub, I have to
move up by 2 folders which is why I have used ../ twice in
Server.MapPath but ASP.NET generates the following error:

Cannot use a leading .. to exit above the top directory.

pointing to the Server.MapPath line. How do I detect the existence of
Form1.asp in C:\Inetpub?

Thanks,

Arpan
 
F

Francois Malgreve

Hi,

Well the problem seems to be a security issue. The root of your application
is probably C:\Inetpub\wwwroot\ASPX and it makes sense that the Application
cannot go above it.
If you want to go above then you need to use standard File / Directory
methods from the System.IO namespace.

If your ASPNET windows user has the proper right you should be able to do
something like the following:

Dim path As String = "C:\Inetpub\Form1.asp"

If File.Exists(path) Then
Response.Write("File Exists")
Else
Response.Write("File Doesnot Exist")
End If

I did not try the code, it's just to give you an idea.

The bottom line is that it makes sense that the method Server.MapPath cannot
go higher in the directory structure than its root. Server.MapPath is used
to translate a virtual path to a physical folder. the physical folder
C:\Inetpub\ does not map to any virtual folder, then it can't work.

I hope this help you understanding the problem.

Best,

Francois Malgreve.
 
P

Patrice

When the "enable parent path" IIS setting is disabled (which is now the
default) .. is not usable in Server.MapPath for security reasons.

Another option is to use Server.MapPath to get the base location first (or
you could get the application root directly).

Then you can alter this location yo get at whetever location you want....
 

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

Similar Threads

System.IO? 9
Server.MapPath & Request.MapPath 4
Business Objects? 2
ASP / ASP.Net server.mappath 4
Disco.exe? 5
javascript to produce a report? 2
loginUrl In Web.config? 1
Response.Write In Class? 1

Top