Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在c中使用线程创建临时文件#_C#_Multithreading_Httphandler - Fatal编程技术网

C# 如何在c中使用线程创建临时文件#

C# 如何在c中使用线程创建临时文件#,c#,multithreading,httphandler,C#,Multithreading,Httphandler,我需要在Handler.ashx文件中创建一个临时文件(new.txt),但问题是我需要将创建的excel文件保存在new.txt中作为临时文件。问题是,我将临时文件硬编码为“new.txt”。如果多个用户访问同一应用程序,将会发生什么。如何克服这个问题。我们可以使用线程吗 示例代码 if (File.Exists(context.Server.MapPath("new.txt"))) { File.Delete(context.Serv

我需要在Handler.ashx文件中创建一个临时文件(new.txt),但问题是我需要将创建的excel文件保存在new.txt中作为临时文件。问题是,我将临时文件硬编码为“new.txt”。如果多个用户访问同一应用程序,将会发生什么。如何克服这个问题。我们可以使用线程吗

示例代码

 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();  

使用
System.IO.Path.GetTempFileName()
Path.GetRandomFileName()
方法。

使用
System.IO.Path.GetTempFileName()
Path.GetRandomFileName()
方法。

+1。他们还希望更改逻辑,以便在操作结束时删除该文件,因为如果该文件已经存在,则当前删除该文件的方法将丢失该文件,因为它将尝试与上一次运行不同的路径。+1。他们还希望更改逻辑,以便在操作结束时删除该文件,因为如果该文件已经存在,则当前删除该文件的方法将丢失该文件,因为它将尝试与上一次运行不同的路径。