Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a temporary file (for writing to) in C#? [duplicate]

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...

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...

like image 785
Daren Thomas Avatar asked Aug 21 '08 14:08

Daren Thomas


People also ask

Which function is used to create a temporary file?

Description. The tmpfile() function creates a temporary binary file. The file is automatically removed when it is closed or when the program is ended. The tmpfile() function opens the temporary file in wb+ mode.

What is an example of a temporary file?

Techopedia Explains Temporary File In terms of backup purposes, Microsoft's Office applications are good examples of this. For example, Microsoft Word and Excel save a temporary file associated with the current open document that it points to after a computer has recovered from a crash or power outage.

How do you create a temporary folder?

Open your File Explorer (it's usually the first button on your desktop taskbar, looks like a folder). Go to the "This PC" section on the left, and then double-click your C: drive. On the Home tab at the top, click "New Folder" and name it "Temp".


1 Answers

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.

An example usage:

using (var tempFile = new TemporaryFile()) {
    // use the file through tempFile.FilePath...
}
like image 79
Jordão Avatar answered Sep 23 '22 11:09

Jordão