0
我需要在我的Handler.ashx文件中創建一個臨時文件(new.txt),但事情是我將創建的excel文件保存在new.txt中作爲臨時文件。問題是在這裏,我將臨時文件硬編碼爲「new.txt」。如果多個用戶訪問相同的應用程序會發生什麼情況。如何克服這個問題。我們可以使用線程。如何使用C#中的線程創建一個臨時文件
樣本代碼..
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();
+1。他們還希望在操作結束時更改他們的邏輯以刪除文件,因爲當前刪除它的方法(如果它已經存在)會錯過它,因爲它將嘗試到上一次運行的不同路徑。 – 2012-01-27 10:45:29