Created
September 22, 2020 15:25
-
-
Save smailliwcs/7f43f0e0428e4ea051942768ff1de81d to your computer and use it in GitHub Desktop.
Specifying extensions for temporary files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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