Larry G. Wapnitsky
Larry G. Wapnitsky

Reputation: 1246

Saving an e-mail attachment to a UNC path

I have the following code in a VSTO add-in I'm writing for Outlook:

        savefolder = Regex.Replace(Guid.NewGuid().ToString(), @"[- ]", String.Empty);

        savepathfull = string.Format(@"{0}{1}", netloc, savefolder);
        DirectoryInfo di = new DirectoryInfo(@savepathfull);
        if (!(di.Exists))
            Directory.CreateDirectory(@savepathfull);



        removedFiles = new List<string>();

        for (int d = attachs.Count; d > 0; d--)
        {
            if (attachs[d].Size > smallAttachment)
            {
                removedFiles.Add(attachs[d].FileName);
                attachs[d].SaveAsFile(savepathfull);
            }
        }

Everything works fine until I try to save the attachment, at which point I receive an UnauthorizedAccessException. I know that my test user has full rights to the folder, yet I still receive this error.

Ideas?

Thanks.

Upvotes: 2

Views: 908

Answers (1)

SliverNinja - MSFT
SliverNinja - MSFT

Reputation: 31641

You need to provide a valid filename when calling Attachment.SaveAsFile. You are trying to save to a directory, not to a file. See MSDN for reference code.

attachs[d].SaveAsFile(Path.Combine(savepathfull, attachs[d].DisplayName);

Upvotes: 6

Related Questions