Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When pressing a button I have to create on client a text file, open it and then delete it. I can create and open/download it with:

protected void btNotepadPending_Click(object sender, EventArgs e)
{
    string sFileName = System.IO.Path.GetRandomFileName();      
    try
    {
        String testo = "XXX" + Environment.NewLine;
        int iii = 0;
        foreach (var item in lUserPending)
            testo += ++iii + " - " + item.Item1 + " " + item.Item2;


        //Create and populate a memorystream 
        MemoryStream mstream = GetTextAsStream(testo);

        //Convert the memorystream to an array of bytes.
        byte[] byteArray = mstream.ToArray();

        //Clean up the memory stream
        mstream.Flush();
        mstream.Close();

        // Clear all content output from the buffer stream
        Response.Clear();

        // Add a HTTP header to the output stream that specifies the default filename
        // for the browser's download dialog
        Response.AddHeader("Content-Disposition", "attachment; filename=delenda.txt");


        // Add a HTTP header to the output stream that contains the
        // content length(File Size). This lets the browser know how much data is being transfered
        Response.AddHeader("Content-Length", byteArray.Length.ToString());


        // Set the HTTP MIME type of the output stream
        Response.ContentType = "application/octet-stream";

        // Write the data out to the client.
        Response.BinaryWrite(byteArray);
        Response.Flush();
        Response.Close();
    }
    catch (Exception exc)
    {
        MessageBox.SendErrorEmail("Exception: " + exc);
    }
}

private MemoryStream GetTextAsStream(String text)
{
    //Create the return memorystream object
    MemoryStream ReturnStream = new MemoryStream();

    //Create a streamwriter to write to the memory stream
    StreamWriter WriteStream = new StreamWriter(ReturnStream);

    //Write the text in the textbox to the Memory Stream.
    WriteStream.WriteLine(text);

    //Clean up the stream writer
    WriteStream.Flush();
    WriteStream.Close();

    //Return the memory Stream
    return ReturnStream;

}


but how to delete it? I tried to put a delete when exiting on


C#
protected override void OnUnload(EventArgs e)
{
    if (IsPostBack)
    {
        if (File.Exists("delenda.txt"))
            File.Delete("delenda.txt");
    }
}


but it never stopped here. So how can I now when notepad (or any other associated program is closed?)
Posted
Comments
[no name] 2-Oct-15 23:06pm    
You wrote code to delete the text file then where is the problem. Are you getting any exception. Please elaborate more by using improve question..
Patrick70__ 4-Oct-15 10:32am    
Yep sorry my bad. If I delete if after the response.close() I'm getting this exception:


Errore scrittura StoricoGare.txt: System.Threading.ThreadAbortException: Thread was being aborted.
at System.Threading.Thread.AbortInternal()
at System.Threading.Thread.Abort(Object stateInfo)
at System.Web.HttpResponse.AbortCurrentThread()
at System.Web.HttpResponse.End()
at System.Web.HttpServerUtility.Transfer(String path, Boolean preserveForm)
at Altro_Admin_Admin_President.btSetCompetition_Click(Object sender, EventArgs e)

1 solution

Your code isn't creating a file on the client, it is offering the client the opportunity to download the file. You can't create files on the client and you can't delete them either. Would you want web sites creating and deleting files on your file system?
 
Share this answer
 
Comments
Patrick70__ 4-Oct-15 10:41am    
Now that you are telling me that is sooooo simple!!!! No of course no one (except virus tester) would want servers to delete files from clients.
One more question please. My purpose was to provide the user with some information. For that I use a button with the aforementioned code. Could you please elaborate a different -smarter- strategy to do that? Thanx Patrick

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900