进程无法访问文件附件

时间:2016-05-26 14:56:01

标签: c# file file-io attachment

我有以下代码(或其等价物):

var provider = new System.Net.Http.MultipartFormDataStreamProvider([...]);
try
{
    if (provider.FileData.Count != 0)
    {
        foreach (System.Net.Http.MultipartFileData file in provider.FileData)
        {
            Trace.WriteLine(file.Headers.ContentDisposition.FileName);
            Trace.WriteLine("Server file path: " + file.LocalFileName);

            AddAttachment(file.LocalFileName, file.Headers.ContentDisposition.FileName.Replace("\"", ""));

            if (attachmentPaths.Length != 0)
            {
                attachmentPaths += ";";
            }
            attachmentPaths += file.Headers.ContentDisposition.Name.Replace("\"", "");
        }
    }
}
catch (Exception ex)
{ ... }

...
public void AddAttachment(string attachmentFileName, string attachmentName)
{
    var AttachmentList = new List<System.Net.Mail.Attachment>();

    System.Net.Mail.Attachment newAttachment = new System.Net.Mail.Attachment(attachmentFileName);
    newAttachment.Name = attachmentName;
    AttachmentList.Add(newAttachment);
}

我得到的“进程无法访问该文件......因为它正被另一个进程使用”错误。我只想知道我的应用程序可能会出错。我无法分辨它在这里实际上是在尝试访问该文件。我最好的猜测是在Attachment构造函数中,但我真的不确定。

1 个答案:

答案 0 :(得分:0)

我能够找到一种在调试器中测试它的方法。答案是肯定的,System.Net.Mail.Attachment构造函数是试图访问该文件的内容。