0

I'm attempting to use a console application to attach a file to a list item I have created in code. Here's what I have:

var attachmentInfo = new AttachmentCreationInformation();
               // attachmentInfo.FileName = System.IO.Path.GetFileName(pdfPath);
                attachmentInfo.FileName = System.IO.Path.GetFileName(pdfPath);
                using (var fs = new FileStream(pdfPath, FileMode.Open))
                {
                    attachmentInfo.ContentStream = fs;
                    var attachment = oListItem.AttachmentFiles.Add(attachmentInfo);
                   // ctx.ExecuteQuery();
                   // return attachment;
                }

The using statement fails with the following error:

Could not find file 

I have hardcoded the filepath plus the file name into a constant called "pdfPath". No luck. I have searched and found several code examples, but none work. I know I create the AttachmentCreationInformation object, but what do I specifically populate it with? That's the point where I'm getting confused. Does anyone have a working sample?

1

Folks figured out my issue. The problem was the location of the file. This code worked:

var attInfo = new AttachmentCreationInformation();
attInfo.FileName = StringFile.DestinationFileName;
attInfo.ContentStream = new MemoryStream(System.IO.File.ReadAllBytes(StringFile.FileAtDestinationPath));
Attachment att = item.AttachmentFiles.Add(attInfo);
ctx.Load(att);
ctx.ExecuteQuery();

The difference is that I copied the file to a folder on my local (c) drive. The problem was that every example I looked at just mentioned filepath nothing more specific. The variable stringfile.filedestinationPath refers to the file on my local drive. Hope this helps someone else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.