asp.net IO异常:进程无法访问该文件,因为它正在被使用

wbgh16ku  于 2023-03-24  发布在  .NET
关注(0)|答案(1)|浏览(193)

自从asp.net升级到net7.0后,我的www.example.com网站出现了问题。
我将一个pdf加载到临时目录中,并想将其附加到电子邮件中。

// upload invoice to computer
        var httpClient = new HttpClient();
        var responseStream = await httpClient.GetStreamAsync("..site.com/.../uploads/" + invoice.PfadRel + "");
        using var fileStream = new FileStream(@"C:\Temp\Invoice-" + invoice.no + ".pdf", FileMode.Create, (FileAccess)FileShare.ReadWrite);
        responseStream.CopyTo(fileStream);

        // attach invoice to mail
        builder.Attachments.Add(@"C:\Temp\Invoice-" + invoice.no + ".pdf");

不幸的是,带有“builder.Attachments.”的行生成错误消息:

IOException: The process cannot access the file because it is being used by another process.

有人知道这里出了什么问题吗?

pieyvz9o

pieyvz9o1#

@CodeMaster:这就是暗示。
这解决了我的问题:

using var fileStream = new FileStream... ;
        {
            responseStream.CopyTo(fileStream);
            fileStream.Dispose();
        }

相关问题