SPFile.CopyTo Method (String)

Copies the file to the destination URL but does not overwrite an existing file of the same name.

Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)


Public Sub CopyTo ( _
    strNewUrl As String _

Dim instance As SPFile
Dim strNewUrl As String

public void CopyTo(
    string strNewUrl


  • strNewUrl
    Type: System.String
    A string that specifies the destination URL.


The CopyTo method copies files to another location within the same site. To copy files to a location in another site, use one of the Add methods of the SPFileCollection class.

This method returns an error if a file of the same name already exists at the new location.


The following code example copies files from one document library into another if the files are checked into the original library with the comment "Complete".

Dim site As SPSite = SPContext.Current.Site
Dim web As SPWeb = site.AllWebs("Site_Name")
Dim files As SPFileCollection = 
Dim intIndex As Integer

For intIndex = 0 To files.Count - 1

    If files(intIndex).CheckInComment = "Complete" Then
        files(intIndex).CopyTo("Destination_DocLibrary/" & 
    End If

Next intIndex
SPSite oSiteCollection = SPContext.Current.Site;
using(SPWeb oWebsite = oSiteCollection.AllWebs["Site_Name"])
    SPFileCollection collFiles =   

    for (int intIndex=0; i<collFiles.Count; intIndex++)
        if (collFiles[intIndex].CheckInComment == "Complete")
            colFiles[intIndex].CopyTo("Destination_DocLibrary/" + 


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 Best Practices: Using Disposable Windows SharePoint Services Objects.

See Also


SPFile Class

SPFile Members

CopyTo Overload

Microsoft.SharePoint Namespace