Compartilhar via


SPFile.MoveTo Method (String, Boolean)

Moves the file to the destination URL and optionally overwrites an existing file of the same name.

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

Syntax

'Declaration
Public Sub MoveTo ( _
    newUrl As String, _
    bOverWrite As Boolean _
)
'Usage
Dim instance As SPFile
Dim newUrl As String
Dim bOverWrite As Boolean

instance.MoveTo(newUrl, bOverWrite)
public void MoveTo(
    string newUrl,
    bool bOverWrite
)

Parameters

  • newUrl
    Type: System.String

    The new destination URL for the file.

  • bOverWrite
    Type: System.Boolean

    true to overwrite an existing file of the same name; otherwise, false.

Exceptions

Exception Condition
SPException

The bOverWrite parameter is false and a file with the same name exists at the specified destination URL

-or-

An error occurred when moving the file.

Remarks

The MoveTo method is used to move files to another location within the same site.

Examples

The following code example moves all files by a specified author from one folder to another folder. The For loop in the example decrements (intIndex--) instead of increments (intIndex++), since the files are being moved from the folder and the number of files decreases with each loop.

Dim site As SPSite = SPContext.Current.Site
Dim web As SPWeb = site.AllWebs("Site_Name")
Dim files As SPFileCollection = web.GetFolder("Source_Folder").Files
Dim i As Integer

For i = files.Count - 1 To 0 Step -1

    If files(i).Author.LoginName = "User_Name" Then

        files(i).MoveTo("Destination_Folder/" & files(i).Name, True)

    End If

Next i
SPSite oSiteCollection = SPContext.Current.Site;
SPWeb oWebsite = oSiteCollection.AllWebs["Site_Name"];
SPFileCollection collFiles = oWebsite.GetFolder("Source_Folder").Files;
for (int intIndex=collFiles.Count-1; intIndex>-1; intIndex--)
{
    if (collFiles[intIndex].Author.LoginName == "User_Name")
    {
        collFiles[intIndex].MoveTo("Destination_Folder/" + files[intIndex].Name, true);
    }
}
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

SPFile Class

SPFile Members

MoveTo Overload

Microsoft.SharePoint Namespace