我有这个 VB 代码可以从我的网络服务器中删除垃圾文件。该代码似乎运行良好,我想通过使用 sql 作业自动化此代码以每天运行它。怎么可能呢?
这是代码:
Public Sub GetFilesAndFolders()
Dim dirInfo As DirectoryInfo = New DirectoryInfo(Server.MapPath("~/test"))
Dim fileInfo() As FileInfo = dirInfo.GetFiles("*.*", SearchOption.AllDirectories)
Dim fileExist As ArrayList = New ArrayList()
Dim pathExist As ArrayList = New ArrayList()
Dim fileToDelete As ArrayList = New ArrayList()
pathExist = returnPathExist()
For Each d As FileInfo In fileInfo
If String.Format("{0:dd/MM/yyyy}", d.CreationTime) = Date.Today Then
fileExist.Add("~\test\" & d.Name)
End If
Next
For Each listElement As String In fileExist
If Not pathExist.Contains(listElement) Then
fileToDelete.Add(listElement)
End If
Next
For Each item As String In fileToDelete
Dim imageFile As FileInfo = New FileInfo(HttpContext.Current.Server.MapPath(item))
If imageFile.Exists Then
File.Delete(HttpContext.Current.Server.MapPath(item))
End If
Next
End Sub
Function returnPathExist() As ArrayList
Dim pathExist As ArrayList = New ArrayList()
'declare connection to the database and stored procedure
Try
cnn.Open()
rdr = cmd.ExecuteReader
While rdr.Read()
pathExist.Add(rdr("test"))
End While
rdr.Close()
Finally
cnn.Close()
cnn.Dispose()
End Try
Return pathExist
End Function
我想使用 CLR,但不确定应该添加哪个 CLR。模块、类、存储过程或??? 而且 CLR 不支持 httpcontext 和 system.Configuration.ConfigurationManager ,我需要在上面的 returnPathExist 函数中访问我的存储过程。
谢谢你的建议。
更新:
我离让它发挥作用只有一步之遥。以下是引发错误的行:
Dim dirInfo As DirectoryInfo = New DirectoryInfo(Server.MapPath("~/test"))
Dim imageFile As FileInfo = New FileInfo(HttpContext.Current.Server.MapPath(item))
File.Delete(HttpContext.Current.Server.MapPath(item))
看来 HttpContext.Current 不能在 CLR 中使用。这是真的吗?有什么办法可以解决这个问题吗?
谢谢。