SPFile.MoveTo Method (String)
Moves the file to the destination URL but does not overwrite an existing file of the same name.
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online
Parameters
- newUrl
- Type: System.String
A string that specifies the destination URL.
| Exception | Condition |
|---|---|
| SPException |
A file of that name already exists at the specified location. -or- An error occurred when moving the file. |
The following code example moves files older than a specified date from one folder to another folder.
SPSite oSiteCollection = SPContext.Current.Site; SPWeb oWebsite = oSiteCollection.AllWebs["Site_Name/Subsite_Name"]; SPFileCollection collFiles = oWebsite.GetFolder("Source_Folder").Files; for (int intIndex=collFiles.Count-1; intIndex>-1; intIndex--) { if (collFiles[intIndex].TimeCreated < Convert.ToDateTime("12/01/2007 12:00:00 AM")) { collFiles[intIndex].MoveTo("Destination_Folder/" + collFiles[intIndex].Name); } } 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. |
Note