When to delete generated file using asp.net
<p>I have a template excel file to generate excel files from it.</p>

<p>My code is as follows (This part is to create a new excel file from the template):</p>

<pre><code>string currentFN = PropertyFinalResult[0].Fecha;
        string fixCurrentFN = currentFN.Replace('/', '_');

        string currentTime = DateTime.Now.ToLongTimeString();
        string fixCurrentTime = currentTime.Replace(':', '_');
        string addToFileName = fixCurrentTime.Replace(' ', '_');

        string newFN = fixCurrentFN + "-" + addToFileName;

        string SourceFile = Request.PhysicalApplicationPath + "Template\\ExcelTemplate.xlsx";
        string DestFile = Request.PhysicalApplicationPath + "Template\\" + newFN + ".xlsx";
        //To keep FileName for posterior deletion
        Session["sDestFile"] = DestFile;

        try
        {                
            File.Copy(SourceFile, DestFile);                
        }
        catch (Exception ex)
        {
            lblErrorSavingToDB.Text = "Error: " + ex.Message;
            lblErrorSavingToDB.Visible = true;
        }
</code></pre>

<p>after that I open the new excel file, insert the records in it and then, stream the file to the user by doing this:</p>

<pre><code>//Streaming file to client                    
                string fileName = newFN + ".xlsx";
                Response.Redirect("../Template/" + fileName);
</code></pre>

<p>Now, my question is, whether the user save or not the file, when should I delete the generated file? I would prefer once the user closes the popup window regarding Open or Save the file. But how to know when the user closes that window?</p>
