如何在c#中使用线程创建临时文件
我需要在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 System.IO.Path.GetTempFileName() 或 Path.GetRandomFileName() 方法。
Use
System.IO.Path.GetTempFileName()
orPath.GetRandomFileName()
method.