<p>I'm looking for something like the <code>tempfile</code> 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...</p>
<p>It seems, .NET does not have the &quot;batteries included&quot; features of the <code>tempfile</code> 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 (<code>mkstemp()</code> I think): <a href="https://docs.python.org/library/tempfile.html" rel="nofollow noreferrer">https://docs.python.org/library/tempfile.html</a></p>
<hr />
<p>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...</p>

[{'answer_id': 20150, 'author': 'Rob Cooper', 'author_id': 832, 'author_profile': 'https://Stackoverflow.com/users/832', 'pm_score': 0, 'selected': False, 'text': '<p>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..</p>\n\n<p>Obviously it depends on the type of data you want to write to it, and the "security" required..</p>\n\n<p><a href="http://www.developerfusion.co.uk/show/3913/" rel="nofollow noreferrer">This article</a> on DevFusion may be a good place to start?</p>\n'}, {'answer_id': 20153, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<p>Path.GetTempFileName and Path.GetTempPath.  Then you can use <a href="https://web.archive.org/web/20091108030151/http://www.csharphelp.com/archives2/archive315.html" rel="nofollow noreferrer">this link</a> to read/write encrypted data to the file.</p>\n\n<p>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.</p>\n\n<p>Edit:  About the race condition... You could use GetTempPath, then create a temporary filename by using </p>\n\n<pre><code>Path.Combine(Path.GetTempPath(), Path.ChangeExtension(Guid.NewGuid().ToString(), ".TMP"))\n</code></pre>\n'}, {'answer_id': 3378474, 'author': 'Jordão', 'author_id': 31158, 'author_profile': 'https://Stackoverflow.com/users/31158', 'pm_score': 5, 'selected': False, 'text': "<p>I've also had the same requirement before, and I've created a small class to solve it:</p>\n\n<pre><code>public sealed class TemporaryFile : IDisposable {\n  public TemporaryFile() : \n    this(Path.GetTempPath()) { }\n\n  public TemporaryFile(string directory) {\n    Create(Path.Combine(directory, Path.GetRandomFileName()));\n  }\n\n  ~TemporaryFile() {\n    Delete();\n  }\n\n  public void Dispose() {\n    Delete();\n    GC.SuppressFinalize(this);\n  }\n\n  public string FilePath { get; private set; }\n\n  private void Create(string path) {\n    FilePath = path;\n    using (File.Create(FilePath)) { };\n  }\n\n  private void Delete() {\n    if (FilePath == null) return;\n    File.Delete(FilePath);\n    FilePath = null;\n  }\n}\n</code></pre>\n\n<p>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 <code>Dispose</code> or the destructor), it deletes the file. You get the name of the file created (and path) through the <code>FilePath</code> property. You can certainly extend it to also open the file for writing and return its associated <code>FileStream</code>.</p>\n\n<p>An example usage:</p>\n\n<pre><code>using (var tempFile = new TemporaryFile()) {\n    // use the file through tempFile.FilePath...\n}\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/20146', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2260/']