返回FileStream后无法删除临时文件

Bum*_*per 6 c# asp.net-mvc file-io filestream temporary-files

我在C#MVC应用程序中有一个函数,它创建一个临时目录和一个临时文件,然后使用FileStream打开文件,将FileStream返回给调用函数,然后需要删除临时文件.但是,我不知道如何删除临时目录和文件,因为它总是错误地说"进程无法访问该文件,因为它正被另一个进程使用".这是我尝试过的,但FileStream仍在使用finally块中的临时文件.如何返回FileStream并删除临时文件?

public FileStream DownloadProjectsZipFileStream()
{
    Directory.CreateDirectory(_tempDirectory);
    // temporary file is created here
    _zipFile.Save(_tempDirectory + _tempFileName);

    try
    {
        FileStream stream = new FileStream(_tempDirectory + _tempFileName, FileMode.Open);
        return stream;
    }
    finally
    {
        File.Delete(_tempDirectory + _tempFileName);
        Directory.Delete(_tempDirectory);
    }
}
Run Code Online (Sandbox Code Playgroud)

FileStream返回的函数如下所示:

public ActionResult DownloadProjects ()
{
    ProjectDownloader projectDownloader = new ProjectDownloader();

    FileStream stream = projectDownloader.DownloadProjectsZipFileStream();
    return File(stream, "application/zip", "Projects.zip");
}
Run Code Online (Sandbox Code Playgroud)

更新:我忘了提到zip文件是380 MB.使用MemoryStream时,我遇到了系统内存不足的问题.

Jas*_*son 6

这是我使用的上面的缩减版本:

public class DeleteAfterReadingStream : FileStream
{
    public DeleteAfterReadingStream(string path)
        : base(path, FileMode.Open)
    {
    }

    protected override void Dispose(bool disposing)
    {
        base.Dispose(disposing);
        if (File.Exists(Name))
            File.Delete(Name);
    }
}
Run Code Online (Sandbox Code Playgroud)


Dam*_*ver 5

您可以创建一个包装类来实现Stream合同并包含FileStream内部,以及维护文件的路径。

所有标准Stream方法和属性都将传递给FileStream实例。

当此包装类为Disposed 时,您将(在Disposeing 包装后FileStream)然后删除该文件。


Chr*_*Ray 5

我使用了 Damien_The_Unknowner(已接受的答案)的建议,将其写下来,效果非常好。只是想我会分享课程:

public class BurnAfterReadingFileStream : Stream
{
    private FileStream fs;

    public BurnAfterReadingFileStream(string path) { fs = System.IO.File.OpenRead(path); }

    public override bool CanRead { get { return fs.CanRead; } }

    public override bool CanSeek { get { return fs.CanRead; } }

    public override bool CanWrite { get { return fs.CanRead; } }

    public override void Flush() { fs.Flush(); }

    public override long Length { get { return fs.Length; } }

    public override long Position { get { return fs.Position; } set { fs.Position = value; } }

    public override int Read(byte[] buffer, int offset, int count) { return fs.Read(buffer, offset, count); }

    public override long Seek(long offset, SeekOrigin origin) { return fs.Seek(offset, origin); }

    public override void SetLength(long value) { fs.SetLength(value); }

    public override void Write(byte[] buffer, int offset, int count) { fs.Write(buffer, offset, count); }

    protected override void Dispose(bool disposing)
    {
        base.Dispose(disposing);
        if (Position > 0) //web service quickly disposes the object (with the position at 0), but it must get rebuilt and re-disposed when the client reads it (when the position is not zero)
        {
            fs.Close();
            if (System.IO.File.Exists(fs.Name))
                try { System.IO.File.Delete(fs.Name); }
                finally { }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)