SPFileCollection.Delete 方法

删除位于指定 URL 的文件。

命名空间:  Microsoft.SharePoint
程序集:  Microsoft.SharePoint(位于 Microsoft.SharePoint.dll 中)

语法

声明
Public Sub Delete ( _
    urlOfFile As String _
)
用法
Dim instance As SPFileCollection
Dim urlOfFile As String

instance.Delete(urlOfFile)
public void Delete(
    string urlOfFile
)

参数

异常

异常 条件
SPException

执行操作时遇到错误。

备注

此方法删除位于从文件集合urlOfFile参数所指定的 URL 的文件。

示例

下面的代码示例循环访问所有文件在共享文档文档库中的所有子网站的网站和上周内未修改的文件。

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

另请参阅

引用

SPFileCollection 类

SPFileCollection 成员

Microsoft.SharePoint 命名空间