Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

FileStream cannot be closed when an exception is occurred in runtime

The following is a part of my code:

try
{
    using (ExcelEngine excelEngine = new ExcelEngine())
    {
        .
        .
        .
        var path = Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\Data\Temp", userId + ".xlsx");

        //Load the existing Excel workbook into IWorkbook
        FileStream inputStream = new FileStream(path, FileMode.Open);
        .
        .
        .
        //***For example, an exception is occurred here in runtime***
        .
        .
    }
}
catch(Exception ex)
{
    ...
}

I don’t have access to Close() method of FileStream in catch section.
I have to restart the server to close FileStream. How can I fix this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use the using statement for the filestream:

using var inputStream = new FileStream(path, FileMode.Open);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading