我的项目中有一个问题是找到网站的上次修改日期..
是在asp.net中找到的任何代码
提前感谢..
答案 0 :(得分:1)
查看此问题
How can you mine the "Last Modified Date" for an ASP.NET page which is based on a Master Page?
您需要的基本代码是
Dim strPath As String = Request.PhysicalPath
Label1.Text = "Modified: " + System.IO.File.GetLastWriteTime(strPath).ToString()
答案 1 :(得分:0)
FileInfo.LastWriteTime应该能满足您的需求:
System.IO.File.GetLastWriteTime(Request.PhysicalPath).ToString();
根据您对其他答案的评论,您希望获得任何网站的最后修改时间(而不是您自己的ASP.NET页面)。您可以使用Net.HttpWebRequest请求指定的网址获取LastModified的HttpResponse属性:
Protected Sub GetLastModifiedTimeOfWebPage(sender As Object, e As EventArgs)
Dim url = Me.TxtURL.Text.Trim
If Not url.StartsWith("http:") Then url = "http://" & url
Dim ResponseStatus As System.Net.HttpStatusCode
Dim lastModified As Date
Try
lastModified = RequestLastModified(url, ResponseStatus)
Catch ex As System.Exception
' log and/or throw
Throw
End Try
If ResponseStatus = Net.HttpStatusCode.OK Then
Me.LblLastModified.Text = lastModified.ToString
End If
End Sub
Public Shared Function RequestLastModified( _
ByVal URL As String, _
ByRef retStatus As Net.HttpStatusCode
) As Date
Dim lastModified As Date = Date.MinValue
Dim req As System.Net.HttpWebRequest
Dim resp As System.Net.HttpWebResponse
Try
req = DirectCast(Net.HttpWebRequest.Create(New Uri(URL)), Net.HttpWebRequest)
req.Method = "HEAD"
resp = DirectCast(req.GetResponse(), Net.HttpWebResponse)
retStatus = resp.StatusCode
lastModified = resp.LastModified
Catch ex As Exception
Throw
Finally
If resp IsNot Nothing Then
resp.Close()
End If
End Try
Return lastModified
End Function
注意:许多网站都使用此属性,并仅返回当前时间。