共用方式為


SPFileCollection.Delete Method

Deletes the file located at the specified URL.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online

Syntax

'Declaration
Public Sub Delete ( _
    urlOfFile As String _
)
'Usage
Dim instance As SPFileCollection
Dim urlOfFile As String

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

Parameters

Exceptions

Exception Condition
SPException

An error was encountered while performing the operation.

Remarks

This method deletes the file located at the URL specified by the urlOfFile parameter from the file collection.

Examples

The following code example iterates through all the files in the Shared Documents document library of every subsite for a site and deletes files that were not modified within the last week.

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();
    }
}

Note

Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. For information about good coding practices, see Disposing Objects.

See Also

Reference

SPFileCollection Class

SPFileCollection Members

Microsoft.SharePoint Namespace