SPFileCollection.Add method (String, Byte[], SPUser, SPUser, DateTime, DateTime)
使用指定的 URL,位元組陣列,包含檔案的內容,表示建立和上次修改檔案,並且時一樣,指定的DateTime值的使用者的使用者物件的集合中建立的檔案。
Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Syntax
'宣告
Public Function Add ( _
urlOfFile As String, _
file As Byte(), _
createdBy As SPUser, _
modifiedBy As SPUser, _
timeCreated As DateTime, _
timeLastModified As DateTime _
) As SPFile
'用途
Dim instance As SPFileCollection
Dim urlOfFile As String
Dim file As Byte()
Dim createdBy As SPUser
Dim modifiedBy As SPUser
Dim timeCreated As DateTime
Dim timeLastModified As DateTime
Dim returnValue As SPFile
returnValue = instance.Add(urlOfFile, _
file, createdBy, modifiedBy, timeCreated, _
timeLastModified)
public SPFile Add(
string urlOfFile,
byte[] file,
SPUser createdBy,
SPUser modifiedBy,
DateTime timeCreated,
DateTime timeLastModified
)
參數
urlOfFile
Type: System.String檔案的站台相對 URL。
file
Type: []位元組陣列,包含檔案。
createdBy
Type: Microsoft.SharePoint.SPUser建立檔案的使用者。
modifiedBy
Type: Microsoft.SharePoint.SPUser上次修改檔案的使用者。
timeCreated
Type: System.DateTime日期和時間時建立檔案。若要設定建立的時間,您必須也並且設定此值與檔案相關聯之清單項目的索引子透過呼叫Update方法。
timeLastModified
Type: System.DateTime日期和時間上次修改檔案。若要設定上次修改時間,您必須也設定此值,透過與檔案相關聯之清單項目的索引子,呼叫Update方法。
傳回值
Type: Microsoft.SharePoint.SPFile
新加入的檔案。
備註
這個方法無法覆寫檔案,如果檔案已經存在。如果執行命令的使用者不是系統管理員網站群組的成員,就不適用的SPUser物件和DateTime的值。
這個Add方法適用於網站文件庫。不過,如果您使用具有區域文件庫,呼叫會失敗,「 拒絕存取 」 的錯誤。如果您未在Add方法中包含最後四個參數,將檔案加入至區域的文件庫,但您不能變更的作者和文件的時間。
Examples
下列程式碼範例將複製每個檔案在一個網站中的文件庫的最上層資料夾中,另一個網站的文件庫。範例保留的時間建立和上次修改的原始程式庫的值。
Dim site As SPSite = SPContext.Current.Site
Try
Dim srcFolder As SPFolder = site.AllWebs("MySourceWebSite").GetFolder("MySourceDocLib")
Dim destFiles As SPFileCollection = site.AllWebs("MyDestinationWebSite").GetFolder("MyDestinationDocLib").Files
Dim srcFile As SPFile
For Each srcFile In srcFolder.Files
Dim destURL As String = destFiles.Folder.Url + "/" + srcFile.Name
Dim binFile As Byte() = srcFile.OpenBinary()
Dim userAuthor As SPUser = srcFile.Author
Dim userModified As SPUser = srcFile.ModifiedBy
Dim created As System.DateTime = srcFile.TimeCreated
Dim modified As System.DateTime = srcFile.TimeLastModified
Dim newFile As SPFile = destFiles.Add(destURL, binFile, userAuthor, userModified, created, modified)
Dim fileItem As SPListItem = newFile.Item
fileItem("Created") = created
fileItem("Modified") = modified
fileItem.Update()
Next srcFile
Finally
site.Dispose()
End Try
SPSite oSiteCollection = SPContext.Current.Site;
SPFolder oFolder = oSiteCollection.AllWebs["SourceWebSite"].GetFolder("SourceDocLib");
SPFileCollection collFiles = oSiteCollection.AllWebs["DestWebSite"].GetFolder("DestDocLib").Files;
foreach (SPFile oFile in collFiles.Files)
{
string strDestUrl = collFiles.Folder.Url + "/" + srcFile.Name;
byte[] binFile = oFile.OpenBinary();
SPUser oUserAuthor = oFile.Author;
SPUser oUserModified = oFile.ModifiedBy;
System.DateTime dtCreated = oFile.TimeCreated;
System.DateTime dtModified = oFile.TimeLastModified;
SPFile oFileNew = collFiles.Add(strDestUrl, binFile, oUserAuthor, oUserModified, dtCreated, dtModified);
SPListItem oListItem = oFileNew.Item;
oListItem["Created"] = dtCreated;
oListItem["Modified"] = dtModified;
oListItem.Update();
}