Skip to content

Instantly share code, notes, and snippets.

@smailliwcs
Created September 22, 2020 15:25
Show Gist options
  • Save smailliwcs/7f43f0e0428e4ea051942768ff1de81d to your computer and use it in GitHub Desktop.
Save smailliwcs/7f43f0e0428e4ea051942768ff1de81d to your computer and use it in GitHub Desktop.
Specifying extensions for temporary files
using System;
using System.IO;
public static class IOExtensions
{
public static FileInfo GetTemporaryFile(string extension = null)
{
string path = Path.GetTempPath();
FileInfo file;
do
{
string fileName = Path.ChangeExtension(Guid.NewGuid().ToString("N"), extension);
file = new FileInfo(Path.Combine(path, fileName));
} while (file.Exists);
using (file.OpenWrite()) { }
return file;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment