如何在 C# 中创建一个临时文件(用于写入)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a temporary file (for writing to) in C#?
提问by Daren Thomas
Possible Duplicate:
Creating tempory folders
可能重复:
创建临时文件夹
I'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...
我正在寻找类似于tempfile
Python 中的模块的东西:一种(最好)打开文件以写入的安全方式。当我完成后,这应该很容易删除......
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
看来,.NET 没有tempfile
模块的“包含电池”功能,它不仅创建文件,而且将文件描述符(我知道,老派,我知道......)与路径一起返回给它。同时,它确保只有创建用户可以访问文件等等(mkstemp()
我认为):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...
啊,是的,我可以看到。但是 GetTempFileName 确实有一个缺点:在创建文件(调用 GetTempFileName 时会创建一个 0 字节文件)和我打开它(返回 GetTempFileName 之后)之间存在竞争条件。这可能是一个安全问题,尽管不适用于我当前的应用程序...
回答by Rob Cooper
I don't know of any built in (within the framework) classes to do this, but I imagine it wouldn't be too much of an issue to roll your own..
我不知道有任何内置(在框架内)类可以做到这一点,但我想推出自己的类不会有太大问题。
Obviously it depends on the type of data you want to write to it, and the "security" required..
显然,这取决于您要写入的数据类型,以及所需的“安全性”。
This articleon DevFusion may be a good place to start?
回答by Rob Cooper
Path.GetTempFileName and Path.GetTempPath. Then you can use this linkto read/write encrypted data to the file.
Path.GetTempFileName 和 Path.GetTempPath。然后您可以使用此链接将加密数据读/写到文件中。
Note, .NET isn't the best platform for critical security apps. You have to be well versed in how the CLR works in order to avoid some of the pitfalls that might expose your critical data to hackers.
请注意,.NET 不是关键安全应用程序的最佳平台。您必须精通 CLR 的工作方式,以避免一些可能将您的关键数据暴露给黑客的陷阱。
Edit: About the race condition... You could use GetTempPath, then create a temporary filename by using
编辑:关于竞争条件...您可以使用 GetTempPath,然后使用创建一个临时文件名
Path.Combine(Path.GetTempPath(), Path.ChangeExtension(Guid.NewGuid().ToString(), ".TMP"))
回答by Jord?o
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 the FilePath
property. You can certainly extend it to also open the file for writing and return its associated FileStream
.
它会在您指定的文件夹或系统临时文件夹中创建一个临时文件。它是一个一次性类,因此在其生命周期结束时(Dispose
或析构函数),它会删除文件。您可以通过FilePath
属性获得创建的文件的名称(和路径)。您当然可以扩展它以打开文件进行写入并返回其关联的FileStream
.
An example usage:
示例用法:
using (var tempFile = new TemporaryFile()) {
// use the file through tempFile.FilePath...
}