开发者

Can Path.GetTempFileName() ever cache?

开发者 https://www.devze.com 2023-03-19 20:40 出处:网络
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 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 a开发者_运维问答m concerned that the same problem may arise.


Path.GetTempFilename() creates an actual file on disk; depending on what you're doing once you have that filename you could be triggering something. It's written to return an actual file to avoid the race conditions associated with older temp filename methods from the past (notably tmpnam in the C standard library, for example.)

Note that it will throw exceptions if there are more than 65,535 files created with this method without deleting them, or if it can't generate a unique filename.

So, what are you doing with that filename after you get it back from Path.GetTempFilename()?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号