Compartilhar via


SPFile.CopyTo Method (String, Boolean)

Copies 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
<ClientCallableAttribute> _
<ClientCallableExceptionConstraintAttribute(FixedId := "1", ErrorType := GetType(ArgumentException),  _
    Condition := "URL MUST refer to a location within the current Site Collection")> _
<ClientCallableExceptionConstraintAttribute(FixedId := "2", ErrorType := GetType(SPFileCheckOutException),  _
    ErrorCode := , Condition := "The file is already checked out")> _
<ClientCallableExceptionConstraintAttribute(FixedId := "3", ErrorType := GetType(SPFileCheckOutException),  _
    ErrorCode := , Condition := "The file is not checked out")> _
<ClientCallableExceptionConstraintAttribute(FixedId := "9", ErrorType := GetType(SPInvalidLookupValuesException),  _
    ErrorCode := , Condition := "The list item could not be updated because because invalid lookup values were found for one or more field(s) in the list")> _
<ClientCallableExceptionConstraintAttribute(FixedId := "4", ErrorType := GetType(SPFileLockException),  _
    ErrorCode := , Condition := "There is an exclusive lock on the file")> _
<ClientCallableExceptionConstraintAttribute(FixedId := "5", ErrorType := GetType(SPFileLockException),  _
    ErrorCode := , Condition := "There is a shared lock on the file")> _
<ClientCallableExceptionConstraintAttribute(FixedId := "10", ErrorType := GetType(UnauthorizedAccessException),  _
    Condition := "Lack of permission to perform the operation")> _
<ClientCallableExceptionConstraintAttribute(FixedId := "6", ErrorType := GetType(SPFileLockException),  _
    ErrorCode := , Condition := "The operation timed out")> _
<ClientCallableExceptionConstraintAttribute(FixedId := "7", ErrorType := GetType(SPFileLockException),  _
    ErrorCode := , Condition := "The file is not locked")> _
<ClientCallableExceptionConstraintAttribute(FixedId := "8", ErrorType := GetType(SPDuplicateValuesFoundException),  _
    ErrorCode := , Condition := "The list item could not be updated because duplicate values were found for one or more field(s) in the list")> _
Public Sub CopyTo ( _
    strNewUrl As String, _
    bOverWrite As Boolean _
)
'Usage
Dim instance As SPFile
Dim strNewUrl As String
Dim bOverWrite As Boolean

instance.CopyTo(strNewUrl, bOverWrite)
[ClientCallableAttribute]
[ClientCallableExceptionConstraintAttribute(FixedId = "1", ErrorType = typeof(ArgumentException), 
    Condition = "URL MUST refer to a location within the current Site Collection")]
[ClientCallableExceptionConstraintAttribute(FixedId = "2", ErrorType = typeof(SPFileCheckOutException), 
    ErrorCode = , Condition = "The file is already checked out")]
[ClientCallableExceptionConstraintAttribute(FixedId = "3", ErrorType = typeof(SPFileCheckOutException), 
    ErrorCode = , Condition = "The file is not checked out")]
[ClientCallableExceptionConstraintAttribute(FixedId = "9", ErrorType = typeof(SPInvalidLookupValuesException), 
    ErrorCode = , Condition = "The list item could not be updated because because invalid lookup values were found for one or more field(s) in the list")]
[ClientCallableExceptionConstraintAttribute(FixedId = "4", ErrorType = typeof(SPFileLockException), 
    ErrorCode = , Condition = "There is an exclusive lock on the file")]
[ClientCallableExceptionConstraintAttribute(FixedId = "5", ErrorType = typeof(SPFileLockException), 
    ErrorCode = , Condition = "There is a shared lock on the file")]
[ClientCallableExceptionConstraintAttribute(FixedId = "10", ErrorType = typeof(UnauthorizedAccessException), 
    Condition = "Lack of permission to perform the operation")]
[ClientCallableExceptionConstraintAttribute(FixedId = "6", ErrorType = typeof(SPFileLockException), 
    ErrorCode = , Condition = "The operation timed out")]
[ClientCallableExceptionConstraintAttribute(FixedId = "7", ErrorType = typeof(SPFileLockException), 
    ErrorCode = , Condition = "The file is not locked")]
[ClientCallableExceptionConstraintAttribute(FixedId = "8", ErrorType = typeof(SPDuplicateValuesFoundException), 
    ErrorCode = , Condition = "The list item could not be updated because duplicate values were found for one or more field(s) in the list")]
public void CopyTo(
    string strNewUrl,
    bool bOverWrite
)

Parameters

  • strNewUrl
    Type: System.String

    The destination URL of the new file.

  • bOverWrite
    Type: System.Boolean

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

Exceptions

Exception Condition
SPException

An error occurred while copying the file.

Remarks

The CopyTo method copies files to another location on 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 bOverWrite is set to false and a file of the same name already exists at the new location.

Examples

The following code example copies all the files in the Shared Documents document library to another folder on the same site, overwriting any file of the same name.

Dim site As SPSite = SPContext.Current.Site
Dim web As SPWeb = site.AllWebs("Site_Name/Subsite_Name")
Dim files As SPFileCollection = web.GetFolder("Shared Documents").Files
Dim intIndex As Integer

For intIndex = 0 To files.Count - 1

    files(intIndex).CopyTo("Destination_DocLib/" & 
        files(intIndex).Name, True)

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

    for (int intIndex=0; intIndex<collFiles.Count; intIndex++)
    {
        collFiles[intIndex].CopyTo("Destination_DocLib/" + 
            collFiles[intIndex].Name, true);
    }
}

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

CopyTo Overload

Microsoft.SharePoint Namespace