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

发布于 2024-12-29 10:09:35 字数 1235 浏览 3 评论 0原文

我需要在 Handler.ashx 文件中创建一个临时文件(new.txt),但问题是我将 new.txt 中创建的 excel 文件保存为临时文件。问题是在这里,我将临时文件硬编码为“ 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();  

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

很酷不放纵 2025-01-05 10:09:35

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文