使用Using无法读取流>>

问题描述 投票:0回答:1

我通过为file is being used by another process实现using处理FileStream的错误。但是,我遇到了Stream was not readable的错误。

下面是我的代码段:


之前:

正在运行,但遇到“文件正在被另一个进程使用”错误定期

EmailMessage responseMessageWithAttachment = responseMessage.Save();

foreach (var attachment in email.Attachments)
{
    if (attachment is FileAttachment)
    {
        FileAttachment fileAttachment = attachment as FileAttachment;
        fileAttachment.Load();
        fileAttachment.Load(AppConfig.EmailSaveFilePath + fileAttachment.Name);

        FileStream fs = new FileStream(AppConfig.EmailSaveFilePath + fileAttachment.Name, FileMode.OpenOrCreate);
        responseMessageWithAttachment.Attachments.AddFileAttachment(attachment.Name, fs);
    }
}
responseMessageWithAttachment.SendAndSaveCopy();

之后:

遇到“无法读取流”错误

EmailMessage responseMessageWithAttachment = responseMessage.Save();

foreach (var attachment in email.Attachments)
{
    if (attachment is FileAttachment)
    {
        FileAttachment fileAttachment = attachment as FileAttachment;
        fileAttachment.Load();
        fileAttachment.Load(AppConfig.EmailSaveFilePath + fileAttachment.Name);

        using (FileStream fs = new FileStream(AppConfig.EmailSaveFilePath + fileAttachment.Name, FileMode.OpenOrCreate))
        {
            responseMessageWithAttachment.Attachments.AddFileAttachment(attachment.Name, fs);
        };
    }
}
responseMessageWithAttachment.SendAndSaveCopy();

我正在通过实现for FileStream来处理另一个进程正在使用的文件错误。但是,我遇到了Stream无法读取的错误。下面是我的代码段:之前:...

c# filestream
1个回答
0
投票

正在运行,但定期遇到“文件正在被另一个进程使用”错误

© www.soinside.com 2019 - 2024. All rights reserved.