ani
ani

Reputation: 121

how to create a temporary file using threads in c#

i need to create a tmporary file (new.txt) in my Handler.ashx file,but the thing is i to save the created excel file in new.txt as temporary file.The problem is here i am hardcoded the temporary file as "new.txt".If more than one user access the same application what will happen.How to overcome from this problem.Can we use threading.

sample code..

 if (File.Exists(context.Server.MapPath("new.txt")))
            {
                File.Delete(context.Server.MapPath("new.txt"));
                xlWorkBook.SaveAs(context.Server.MapPath("new.txt"), Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);

            }
            if (!File.Exists(context.Server.MapPath("new.txt")))
            {

                xlWorkBook.SaveAs(context.Server.MapPath("new.txt"), Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);

            }

   string file = context.Server.MapPath("new.txt");
            byte[] myByte = File.ReadAllBytes(file);

            File.Delete(context.Server.MapPath("new.txt"));

            context.Response.Clear();
            context.Response.BinaryWrite(myByte);
            context.Response.Flush();
            context.Response.Close();  

Upvotes: 0

Views: 342

Answers (1)

KV Prajapati
KV Prajapati

Reputation: 94653

Use System.IO.Path.GetTempFileName() or Path.GetRandomFileName() method.

Upvotes: 6

Related Questions