ArcGIS Pro 3.0 API Reference Guide
ArcGIS.Core.Data Namespace / Row Class / AddAttachment Method
The attachment to be associated with the row.
Example

In This Topic
    AddAttachment Method (Row)
    In This Topic
    Adds a given attachment to a row. This method must be called on the MCT. Use QueuedTask.Run.
    Syntax
    public long AddAttachment( 
       Attachment attachment
    )
    Public Function AddAttachment( _
       ByVal attachment As Attachment _
    ) As Long

    Parameters

    attachment
    The attachment to be associated with the row.

    Return Value

    The ID of the attachment after it has been associated with the row.
    Exceptions
    ExceptionDescription
    The table or feature class has not been enabled for attachment creation.
    attachment is null.
    attachment's Data, Name or ContentType property is null or an empty string.
    A geodatabase-related exception has occurred.
    Example
    Adding Attachments
    public async Task AddingAttachments()
    {
      await ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
      {
        using (Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri("path\\to\\sde\\file\\sdefile.sde"))))
        using (FeatureClass parkFeatureClass = geodatabase.OpenDataset<FeatureClass>("LocalGovernment.GDB.Park"))
        {
          QueryFilter filter = new QueryFilter { WhereClause = "NUMPARKING > 0" };
    
          using (RowCursor parkingCursor = parkFeatureClass.Search(filter, false))
          {
            while (parkingCursor.MoveNext())
            {
              using (MemoryStream stream = CreateMemoryStreamFromContentsOf("Sample.xml"))
              {
                Attachment attachment = new Attachment("Sample.xml", "text/xml", stream);
    
                using (Row row = parkingCursor.Current)
                {
                  long attachmentId = row.AddAttachment(attachment);
                }
              }
            }
          }
        }
      });
    }
    
    private MemoryStream CreateMemoryStreamFromContentsOf(String fileNameWithPath)
    {
      MemoryStream memoryStream = new MemoryStream();
    
      using (FileStream file = new FileStream(fileNameWithPath, FileMode.Open, FileAccess.Read))
      {
        byte[] bytes = new byte[file.Length];
        file.Read(bytes, 0, (int)file.Length);
        memoryStream.Write(bytes, 0, (int)file.Length);
      }
    
      return memoryStream;
    }
    Requirements

    Target Platforms: Windows 11, Windows 10, Windows 8.1

    See Also