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;
}