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)

Syntax

Public Sub CopyTo ( _
    strNewUrl As String _
)

Dim instance As SPFile
Dim strNewUrl As String

instance.CopyTo(strNewUrl)
public void CopyTo(
    string strNewUrl
)

Parameters

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

Remarks

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.

Examples

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 = 
    web.GetFolder("Source_DocLibrary").Files
Dim intIndex As Integer

For intIndex = 0 To files.Count - 1

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

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

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

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

See Also

Reference

SPFile Class

SPFile Members

CopyTo Overload

Microsoft.SharePoint Namespace