Q:

How to Add attachments to an existing work item in Azure DevOps using c#

Would you please provide a code sample on how to add attachments (text file and an image) to an existing work item in Azure DevOps using C#. 

A:

Here are some blog and cases: 1 .  2  with C# code samples, you can refer to them. 
using (FileStream attStream = new FileStream(FilePath, FileMode. Open, FileAccess. Read))
{

    var att = WitClient. CreateAttachmentAsync(attStream, filePathSplit[filePathSplit. Length – 1]).Result; // upload the file

    JsonPatchDocument patchDocument = new JsonPatchDocument();

    patchDocument. Add(new JsonPatchOperation()
    {
        Operation = Operation. Add,
        Path = “/relations/-“,
        Value = new
        {
            rel = “AttachedFile”,
            url = att. Url,
            attributes = new { comment = “Comments for the file “ + filePathSplit[filePathSplit. Length – 1] }
        }
    });
}

