获取.NET /打开临时文件

编程入门 行业动态 更新时间:2024-10-13 02:19:22
本文介绍了获取.NET /打开临时文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

解决方案

有两种方法如下:

  • Path.GetTempFileName

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

  • 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:

  • Path.GetTempFileName

    This will create a temporary file and return its name.

  • Path.GetRandomFileName

    This will use a cryptographically strong, random string as file name and won't create the file for you.

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 /打开临时文件

本文发布于:2023-07-06 07:41:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1047430.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:临时文件   NET

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!