SPFileCollection.Delete method
刪除位於指定 URL 的檔案。
Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Syntax
'宣告
Public Sub Delete ( _
urlOfFile As String _
)
'用途
Dim instance As SPFileCollection
Dim urlOfFile As String
instance.Delete(urlOfFile)
public void Delete(
string urlOfFile
)
參數
urlOfFile
Type: System.String若要刪除檔案的站台相對 URL。如需有關在SharePoint Foundation中使用的 Url 的形式的資訊,請參閱Forms of URL Strings。
Exceptions
Exception | Condition |
---|---|
SPException | 執行作業時發生錯誤。 |
備註
這個方法會刪除位於 [從檔案集合的urlOfFile參數所指定的 URL 的檔案。
Examples
下列程式碼範例逐一查看所有檔案共享文件文件庫中的每一個子網站的網站,並刪除最後一週內未修改的檔案。
Dim siteCollection As New SPSite("http://MySiteCollection")
Try
Dim delSites As SPWebCollection = siteCollection.AllWebs("MyWebSite").Webs
Dim cutOffDate As DateTime = DateTime.UtcNow.AddDays(- 7)
Dim delSite As SPWeb
For Each delSite In delSites
Dim delFiles As SPFileCollection = delSite.GetFolder("Shared Documents").Files
Dim i As Integer
For i = delFiles.Count - 1 To (- 1) + -1 Step -1
If delFiles(i).TimeLastModified < cutOffDate Then
Dim delURL As String = delFiles(i).Url
Try
delFiles.Delete(delURL)
Catch Else
End Try
End If
Next i
Next delSite
Finally
siteCollection.Dispose()
End Try
using (SPSite oSiteCollection = new SPSite("https://localhost"))
{
SPWebCollection collWebsites = oSiteCollection.AllWebs["MyWebSite"].Webs;
DateTime dtCutoffDate = DateTime.UtcNow.AddDays(-7);
foreach (SPWeb oWebsite in collWebsites)
{
SPFileCollection collFiles = oWebsite.GetFolder("Shared Documents").Files;
for (int intIndex=collFiles.Count - 1; intIndex>-1; intIndex--)
{
if (collFiles[i].TimeLastModified < dtCutoffDate)
{
string strDelUrl = collFiles[intIndex].Url;
try
{
collFiles.Delete(strDelUrl);
}
catch {}
}
}
oWebsite.Dispose();
}
}
注意事項 |
---|
某些物件實作IDisposable介面,並且您必須避免之後不再需要保留這些物件在記憶體中。良好的程式碼撰寫方式的相關資訊,請參閱Disposing Objects。 |