ASP編程要點ABC
使用Server.MapPath
盡可能地使用Server.MapPath()來表示存儲在服務器上的文件,而不要用靜態絕對路徑。因為,如果采用靜態絕對路徑,當web路徑發生變化時,將導致文件路徑表達錯誤,從而不得不修改原靜態路徑。而使用Server.MapPath()表示的路徑就不必再做修改。
比如,以下的代碼就不是好的方法:
< %
whichfile="D:\inetpub\wwwroot\whatever\junk.txt"
set fs = CreateObject("Scripting.FileSystemObject")
Set thisfile = fs.OpenTextFile(whichfile, 1, False)
tempSTR=thisfile.readall
response.write tempSTR
thisfile.Close
set thisfile=nothing
set fs=nothing
% >
建議使用下面的代碼來完成同樣的功能:
< %
whichfile=server.mappath("\whatever\junk.txt")
set fs = CreateObject("Scripting.FileSystemObject")
Set thisfile = fs.OpenTextFile(whichfile, 1, False)
tempSTR=thisfile.readall
response.write tempSTR
thisfile.Close
set thisfile=nothing
set fs=nothing
% >
|