0

I have an odd problem... and I think I have narrowed it down to a point where we create a temporary file using Page.GetTempFileName().

I sometimes get a error message stating that Cannot create a file when that file already exists.

Is there a chance that using Path.GetTempFilename() could generate the same filename and become cached?

I am changing my code to use:

Path.Combine(System.IO.Path.GetTempPath(), string.Concat(Guid.NewGuid().ToString(), ".xls"));

In the hope that this will generate a unique filename, but I am concerned that the same problem may arise.

4

1 回答 1

2

Path.GetTempFilename() 在磁盘上创建一个实际文件;取决于您在拥有该文件名后所做的事情,您可能会触发某些事情。编写它是为了返回一个实际文件,以避免与过去较旧的临时文件名方法相关的竞争条件(例如,特别是 C 标准库中的 tmpnam。)

请注意,如果使用此方法创建的文件超过 65,535 个而不删除它们,或者无法生成唯一的文件名,它将引发异常。

那么,从 Path.GetTempFilename() 中取回该文件名后,您将如何处理该文件名?

于 2011-07-13T15:46:41.580 回答