tags:

views:

10098

answers:

6

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): http://docs.python.org/lib/module-tempfile.html

+11  A: 

That was covered here: http://stackoverflow.com/questions/16656/creating-tempory-folders

Adam Haile
A: 

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 article on DevFusion may be a good place to start?

Rob Cooper
+5  A: 

Path.GetTempFileName and Path.GetTempPath. Then you can use this link to read/write encrypted data to the file.

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.

Edit: About the race condition... You could use GetTempPath, then create a temporary filename by using

Path.Combine(Path.GetTempPath(), Path.Combine(Guid.NewGuid().ToString(), ".TMP"))
Will
A: 

I haven't seen Path.GetTempDirectory, is it not System.IO.GetTempPath?

Biri
+1  A: 

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

Daren Thomas
Sorry, I'm not following. What is the race condition here? And what's the security issue? Is it that someone else could potentially put a lock on the file before you're able to use it?
herbrandson
Someone else could put a symbolic link where your file is (by guessing the temporary file name) and hijack your data etc. I believe you might find some info on search term "symlink attack"
Daren Thomas
+4  A: 

I've also had the same requirement before, and I've created a small class to solve it:

public 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() {
    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...
}
Jordão
finalizers methods like ~TemporaryFile are dangerous, isn't ???
alhambraeidos
Is a sword dangerous? Or is it the samurai that wields it?
Jordão