This article provides an example of how to upload an attachment to an Item, taking into consideration that the file may already exist as an attachment on the item.

Note: There must be a file repository initialized for your SystemWeaver server.


            // Get file to add as attachment
            FileInfo file = new FileInfo("C:\\Temp\\test.txt");

            // Identify if there already is a file with the same name. Names are what we match on.

            // Placeholder for potential previous version of file.
            IswFileRevision previousAttachmentRevision = null;

            // The attachments on the item.
            IswFileRevisions attachments = item.Files;
            foreach (IswFileRevision attachment in attachments)
                if (file.Name.Equals(attachment.Name))
                    // Match found. Break loop.
                    previousAttachmentRevision = attachment;

            // Compare file Hashes to see if there is any change. 
            // Hash for the file.
            string newFileHash = string.Empty;
            if (SWUtility.TryGetFileHash(file.FullName, out newFileHash))
                // Hash for the previous attachment, if any.
                string attachmentHash = string.Empty;

                // Get Hash for attachment if there was a match.  If there is no attachment Hash will be string.Empty
                if (previousAttachmentRevision != null)
                    attachmentHash = previousAttachmentRevision.Hash;

                // If Hashes differ, upload new file. 
                if (!newFileHash.Equals(attachmentHash))
                    // If there was no matching attachment, previousAttachmentRevision will be null...
                    // ...and it will be uploaded as a new file without previous revisions.                
                    IswFileRevision addedFile = item.AddFileByName(file.FullName, previousAttachmentRevision);

                    // Add a comment that shows up in the Change log for the file.
                    addedFile.Comment = "File Added on: " + DateTime.Now;
                // Error handling