how to close File Stream in C#

Please see this code.

string name = dt.Rows[0]["Name"].ToString();
byte[] documentBytes = (byte[])dt.Rows[0]["DocumentContent"];

int readBytes = 0;
//int index = 0;
readBytes = documentBytes.Length;
    using (FileStream fs = new FileStream(name, FileMode.Create, FileAccess.Write, FileShare.Read))

        fs.Write(documentBytes, 0, readBytes);
        //System.Diagnostics.Process prc = new System.Diagnostics.Process();
        //prc.StartInfo.FileName = fs.Name;
        Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
        app.Visible = false;
        Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Open(fs.Name);
        Microsoft.Office.Interop.Excel._Worksheet worksheet = (Excel.Worksheet)workbook.Sheets[1]; // Explicit cast is not required here
     //   lastRow = worksheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell).Row; 

        app.Visible = true;

catch (Exception ex)
    MessageBox.Show("You have clicked more than one time. File is already open.", "WorkFlow", MessageBoxButtons.OK, MessageBoxIcon.Information);

I am opening an excel file using the file stream. Excel is showing up nicely. But I am not able to close file stream. It still comes with a small pop up that shows 'File Now available'. How to get rid of that? I can see fs.Close() and Flush() really not working here. Please help.

Jon Skeet

You're asking Excel to open the file while you still have the stream open. Given that you're just trying to write bytes to it, I'd just use:

// This will close the file handle after writing the data
File.WriteAllBytes(name, documentBytes);

// Then you're fine to get Excel to open it
var app = new Microsoft.Office.Interop.Excel.Application();
app.Visible = false;
var workbook = app.Workbooks.Open(name);


See more on this question at Stackoverflow