获取.NET /打开临时文件 [英] Get/open temp file in .NET

查看:109
本文介绍了获取.NET /打开临时文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我愿做这样的事情的下文。什么函数返回我打开了一个独特的文件?这样我就可以确保它是我的,我不会覆盖任何内容或编写复杂的FN生成/循环

 的BinaryWriter W = GetTempFile(出FN);
w.close();
File.Move(FN,newFn);
 

解决方案

有两种方法如下:

  • <一个href="http://msdn.microsoft.com/en-us/library/system.io.path.gettempfilename%28v=VS.100%29.aspx">Path.GetTempFileName

    这将创建一个临时文件,并返回其名称。

  • <一个href="http://msdn.microsoft.com/en-us/library/system.io.path.getrandomfilename%28v=VS.100%29.aspx">Path.GetRandomFileName

    这将使用保密性强的随机字符串作为文件名,并不会为你的文件。

通常第一种方法就足够了;对于GetRandomFileName该文件说:

  

当你的文件系统的安全性是最重要的,应该用来代替GetTempFileName这种方法。

I would like to do something like the below. What function returns me an unique file that is opened? so i can ensure it is mine and i wont overwrite anything or write a complex fn generate/loop

BinaryWriter w = GetTempFile(out fn);
w.close();
File.Move(fn, newFn);

解决方案

There are two methods for this:

Usually the first method suffices; the documentation for GetRandomFileName says:

When the security of your file system is paramount, this method should be used instead of GetTempFileName.

这篇关于获取.NET /打开临时文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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