共用方式為


SPAttachmentCollection class

表示清單項目的附件集合。

Inheritance hierarchy

System.Object
  Microsoft.SharePoint.SPAttachmentCollection

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

'宣告
Public Class SPAttachmentCollection _
    Implements ICollection, IEnumerable
'用途
Dim instance As SPAttachmentCollection
public class SPAttachmentCollection : ICollection, 
    IEnumerable

備註

使用SPListItem類別的Attachments屬性,傳回的清單項目的附件集合。若要建立附件,請使用Add方法

若要從集合傳回單一附件的檔案名稱中使用索引子。例如,假設集合指派給變數,名為collAttachments ,使用在 C# 的collAttachments[index]或Visual Basic,在collAttachments(index)index所在集合中的附件索引編號。

Examples

下列程式碼範例示範如何將從共用文件的文件庫的所有檔案都附加在清單中會出現在 [網站] 下方的每一個子網站的第一個清單項目。

SPAttachmentCollection類別的Add方法需要做為參數傳遞檔案,以二進位格式。因此,本範例使用OpenBinary方法的SPFile類別若要開啟的資料夾中的每個檔案以二進位格式。

Dim oSiteCollection As SPSite = SPContext.Current.Site
Dim collWebsites As SPWebCollection = oSiteCollection.AllWebs

Dim oWebsite As SPWeb = collWebsites("Site_Name")
Dim oFolder As SPFolder = oWebsite.Folders("Shared Documents")

For Each oWebsiteNext As SPWeb In collWebsites
    Dim oList As SPList = oWebsiteNext.Lists("List_Name")
    Dim collItem As SPListItemCollection = oList.Items
    Dim oListItem As SPListItem = collItem(0)
    Dim collAttachments As SPAttachmentCollection = oListItem.Attachments

    Dim collFiles As SPFileCollection = oFolder.Files

    For Each oFile As SPFile In collFiles
        Dim strFileName As String = oFile.Name
        Dim binFile As Byte() = oFile.OpenBinary()
 
        collFiles.Add(strFileName, binFile)
    Next oFile

    oListItem.Update()
    oWebsiteNext.Dispose()
Next oWebsiteNext

oWebsite.Dispose()
SPSite oSiteCollection = SPContext.Current.Site;
SPWebCollection collWebsites = oSiteCollection.AllWebs;

SPWeb oWebsite = collWebsites["Site_Name"];
SPFolder oFolder = oWebsite.Folders["Shared Documents"];

foreach (SPWeb oWebsiteNext in collWebsites)
{
    SPList oList = oWebsiteNext.Lists["List_Name"];
    SPListItemCollection collItem = oList.Items;
    SPListItem oListItem = collItem[0];
    SPAttachmentCollection collAttachments = oListItem.Attachments;

    SPFileCollection collFiles = oFolder.Files;

    foreach (SPFile oFile in collFiles)
    {
        string strFileName = oFile.Name;

        byte[] binFile = oFile.OpenBinary();

        collFiles.Add(strFileName, binFile);
    }

    oListItem.Update();
    oWebsiteNext.Dispose();
}
oWebsite.Dispose();
注意事項注意事項

某些物件實作IDisposable介面,並且您必須避免之後不再需要保留這些物件在記憶體中。良好的程式碼撰寫方式的相關資訊,請參閱Disposing Objects

Thread safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

請參閱

參照

SPAttachmentCollection members

Microsoft.SharePoint namespace