SPAttachmentCollection 类

表示列表项附件的集合。

继承层次结构

System.Object
  Microsoft.SharePoint.SPAttachmentCollection

命名空间:  Microsoft.SharePoint
程序集:  Microsoft.SharePoint(位于 Microsoft.SharePoint.dll 中)

语法

声明
Public Class SPAttachmentCollection _
    Implements ICollection, IEnumerable
用法
Dim instance As SPAttachmentCollection
public class SPAttachmentCollection : ICollection, 
    IEnumerable

备注

使用SPListItem类的Attachments属性可返回的附件的列表项的集合。若要创建附件,请使用Add方法

使用索引器从集合中返回单个附件的文件名。例如,假定集合分配给名为collAttachments的变量,使用 C# 中的collAttachments[index]或collAttachments(index)Visual Basic,在其中index是集合中的附件的索引号。

示例

下面的代码示例演示如何将共享文档文档库中的所有文件都附加到列表出现在每个网站下的子网站中第一个列表项。

SPAttachmentCollection类的Add方法需要以二进制格式文件作为参数传递。因此,该示例使用SPFile类的OpenBinary方法以二进制格式打开该文件夹中的每个文件。

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();

备注

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.

线程安全性

该类型的任何公共 静态 (已共享 在 Visual Basic 中) 成员都是线程安全的。不保证任何实例成员都是线程安全的。

另请参阅

引用

SPAttachmentCollection 成员

Microsoft.SharePoint 命名空间