SPFileCollection.Add method (String, Byte[])
使用指定的 URL 和位元組陣列,包含檔案集合中建立的檔案。
Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Syntax
'宣告
Public Function Add ( _
urlOfFile As String, _
file As Byte() _
) As SPFile
'用途
Dim instance As SPFileCollection
Dim urlOfFile As String
Dim file As Byte()
Dim returnValue As SPFile
returnValue = instance.Add(urlOfFile, _
file)
public SPFile Add(
string urlOfFile,
byte[] file
)
參數
urlOfFile
Type: System.String檔案的站台相對 URL。
file
Type: []位元組陣列,包含檔案。
傳回值
Type: Microsoft.SharePoint.SPFile
新加入的檔案。
Examples
下列程式碼範例會使用Add方法,將文件庫中的檔案複製到指定的 Web 網站下的每一個子網站的共享文件資料夾。如果相同名稱的檔案已經存在, Add方法會擲回例外狀況。
Dim SiteCollection As New SPSite("http://MySiteCollection")
Try
Dim webSite As SPWeb = siteCollection.AllWebs("MyWebSite")
Dim destSites As SPWebCollection = webSite.Webs
Dim srcFile As SPFile = webSite.GetFile("MyDocLib/MyFile")
Dim destSite As SPWeb
For Each destSite In destSites
Dim destFolder As SPFolder = destSite.GetFolder("Shared Documents")
Dim destFiles As SPFileCollection = destFolder.Files
Dim destURL As String = destFolder.Url + "/" + srcFile.Name
Dim binFile As Byte() = srcFile.OpenBinary()
destFiles.Add(destURL, binFile)
Next destSite
Finally
SiteCollection.Dispose()
End Try
using (SPSite oSiteCollection = new SPSite("https://localhost"))
{
using(SPWeb oWebsite = oSiteCollection.AllWebs["MyWebSite"])
{
SPWebCollection collWebsites = oWebsite.Webs;
SPFile oFile = oWebsite.GetFile("MyDocLib/MyFile");
foreach (SPWeb oWebsiteNext in collWebsites)
{
SPFolder oFolder = oWebsiteNext.GetFolder("Shared Documents");
SPFileCollection collFiles = oFolder.Files;
string strDestUrl = oFolder.Url + "/" + oFile.Name;
byte[] binFile = oFile.OpenBinary();
collFiles.Add(strDestUrl, binFile);
oWebsiteNext.Dispose();
}
}
}
注意事項 |
---|
某些物件實作IDisposable介面,並且您必須避免之後不再需要保留這些物件在記憶體中。良好的程式碼撰寫方式的相關資訊,請參閱Disposing Objects。 |