如何创建一个临时文件(写入)在C#? [英] How to create a temporary file (for writing to) in C#?
问题描述
可能重复:结果
的创建
块引用>
我在寻找类似的
临时文件
模块在Python:打开一个文件用于写入(最好)安全的方式。这应该是很容易删除时,我做的太...
看来,.NET不具备电池列入的<$ C $的特点C>临时文件模块,它不仅创造了该文件,但与路径一起返回的文件描述符(老同学,我知道...)到它。同时,它确保只有创建用户可以访问该文件和诸如此类的东西(
mkstemp()
我认为):的 https://docs.python.org/library/tempfile.html
嗯,是的,我可以看到。但是,GetTempFileName确实有一个缺点:有当文件被创建之间的竞争条件(在调用GetTempFileName 0字节文件被创建的),当我去打开它(GetTempFileName返回之后)。这可能是一个安全问题,虽然不是我的当前应用程序...
解决方案我也有同样的要求之前,和我创建了一个小的类来解决这个问题:
公共密封类TemporaryFile:IDisposable的{
公共TemporaryFile ():
本(Path.GetTempPath()){}
公共TemporaryFile(字符串目录){
创建(Path.Combine(目录,Path.GetRandomFileName()) );
}
〜TemporaryFile(){
删除();
}
公共无效的Dispose(){
删除();
GC.SuppressFinalize(本);
}
公共字符串的文件路径{搞定;私人集; }
私人void创建(字符串路径){
=文件路径路径;
使用(File.Create(文件路径)){};
}
私人无效删除(){
如果(文件路径== NULL)回报;
File.Delete(文件路径);
文件路径= NULL;
}
}
它创建了一个文件夹,在您指定的临时文件或在系统临时文件夹。这是一个一次性的类,所以在其寿命结束时(无论是
处置
或析构函数),它会删除该文件。你得到通过文件路径
属性创建文件(和路径)的名称。你当然可以扩展它也打开该文件进行写入,并返回其关联的FileStream
这是用法示例:
使用(VAR临时文件=新TemporaryFile()){
//通过tempFile.FilePath使用的文件..
}
Possible Duplicate:
Creating tempory foldersI'm looking for something like the
tempfile
module in Python: A (preferably) secure way to open a file for writing to. This should be easy to delete when I'm done too...It seems, .NET does not have the "batteries included" features of the
tempfile
module, which not only creates the file, but returns the file descriptor (old school, I know...) to it along with the path. At the same time, it makes sure only the creating user can access the file and whatnot (mkstemp()
I think): https://docs.python.org/library/tempfile.html
Ah, yes, I can see that. But GetTempFileName does have a drawback: There is a race condition between when the file was created (upon call to GetTempFileName a 0-Byte file gets created) and when I get to open it (after return of GetTempFileName). This might be a security issue, although not for my current application...
解决方案I've also had the same requirement before, and I've created a small class to solve it:
public sealed class TemporaryFile : IDisposable { public TemporaryFile() : this(Path.GetTempPath()) { } public TemporaryFile(string directory) { Create(Path.Combine(directory, Path.GetRandomFileName())); } ~TemporaryFile() { Delete(); } public void Dispose() { Delete(); GC.SuppressFinalize(this); } public string FilePath { get; private set; } private void Create(string path) { FilePath = path; using (File.Create(FilePath)) { }; } private void Delete() { if (FilePath == null) return; File.Delete(FilePath); FilePath = null; } }
It creates a temporary file in a folder you specify or in the system temporary folder. It's a disposable class, so at the end of its life (either
Dispose
or the destructor), it deletes the file. You get the name of the file created (and path) through theFilePath
property. You can certainly extend it to also open the file for writing and return its associatedFileStream
.An example usage:
using (var tempFile = new TemporaryFile()) { // use the file through tempFile.FilePath... }
这篇关于如何创建一个临时文件(写入)在C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!