2012-01-27 9 views
0

Handler.ashx 파일에 tm 임시 파일 (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(); 

답변

6

사용 System.IO.Path.GetTempFileName() 또는 Path.GetRandomFileName() 방법.

+1

+1. 또한 작업이 끝나면 파일을 삭제하기 위해 논리를 변경하려고합니다. 기존의 파일을 삭제하는 현재의 방법은 이전 실행과 다른 경로를 시도하기 때문에 파일을 놓친 것입니다. –

관련 문제