生成一个唯一的临时文件名使用.NET给定的扩展 [英] Generate a unique temporary file name with a given extension using .NET

查看:92
本文介绍了生成一个唯一的临时文件名使用.NET给定的扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  <一href="http://stackoverflow.com/questions/581570/how-can-i-create-a-temp-file-with-a-specific-extension-with-net">How我可以创建具有特定扩展名的临时文件。NET?

Possible Duplicate:
How can I create a temp file with a specific extension with .net ?

这是可能通过调用建立在.NET中的临时文件

It is possible to create a temporary file in .NET by calling

string fileName = System.IO.Path.GetTempFileName();

这将创建一个文件扩展名为.TMP的临时目录中。

This will create a file with a .TMP extension in the temporary directory.

如果你特别希望它有一个不同的扩展?为了这个例子中,可以说,我需要用.TR5结尾的文件。

What if you specifically want it to have a different extension? For the sake of this example, lets say that I needed a file ending with .TR5.

最明显的(和马车)解决方法是调用

The obvious (and buggy) solution is to call

string fileName = Path.ChangeExtension(Path.GetTempFileName(), "tr5"))

这里的问题是:

The problems here are:

  • 在它仍会生成一个空文件(如tmp93.tmp)在临时目录下,现在将流连无限期
  • 在没有出示担保所产生的文件名(tmp93.tr5)是不是已经存在

有一个简单而安全的方式来产生与特定文件exension一个临时文件?

Is there a straightforward and safe way to generate a temporary file with a specific file exension?

推荐答案

请参见:<一href="http://stackoverflow.com/questions/581570/how-can-i-create-a-temp-file-with-a-specific-extension-with-net">How我可以创建具有特定扩展名的临时文件。NET?

这篇关于生成一个唯一的临时文件名使用.NET给定的扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆