Monday, March 26, 2012

MapPath Can Not Find File

I need to open a text file, permission are okay, and the file exists, however the my code states otherwise. Except when I use Response.Write(Server.MapPath("filename"), then the location of the file is written to the browser.

any suggestions?

Code follows:


<%@dotnet.itags.org. Import Namespace="System.IO" %>
<Script language="VBScript" Runat="Server"
Sub Page_Load
Dim objStreamReader As StreamReader
Dim strInput As String

If File.Exists(MapPath("readtest.txt") ) Then
objStreamReader = File.OpenText(MapPath("readtest.txt") )
strInput = objStreamReader.ReadLine()
While strInput <> Nothing
lblContents.Text &= "<li>" & strInput
strInput = objStreamReader.ReadLine()
End While
objStreamReader.Close
Else
'This line writes to the browser even though the file exists.
lblContents.Text = "readtest.txt does not exist!"
End If

End Sub

</Script
<html>
<head><title>OpenText.asp</title></head>
<body bgcolor="#FFFFFF"
<h3>Contents of readTest.txt</h3>
<asp:Label
ID="lblContents"
Runat="Server" /
<br>
'The path of the file is written to the browser.
<%
Response.Write(Server.MapPath("readtest.txt"))
%>
</body>
</html>

Hi,

can you also provide the exact error that ASP.NET throws at you?

Grz, Kris.
Note that your Response.Write and File.Exists lines are calling 2 different functions.
I am not sure what the differences are, but I'd start by outputting the results of the same function.
System.Web.Server.MapPath()
System.Web.UI.Page.MapPath()

0 comments:

Post a Comment