Per TempFileCollection doc in MSDN:
To generate a unique name for a temporary file of a particular file extension, call AddExtension and specifiy the extension of the file name to generate. The AddExtension method will return a string consisting of a full path to a file name of the specified extension in the directory specified by the TempDir property. The AddExtension method will only return one unique file name per file name extension.
TempFileCollection is in System.CodeDom.Compiler
namespace. If you just want to create some temp files, use Path.GetTempFileName.
Console.WriteLine(Path.ChangeExtension(Path.GetTempFileName(), "xml"));
Console.WriteLine(Path.ChangeExtension(Path.GetTempFileName(), "xml"));
Console.WriteLine(Path.ChangeExtension(Path.GetTempFileName(), "xml"));
// You got three different files with .xml extension.
Files created by Path.GetTempFileName()
will NOT be delete automatically. To clean up temp files automatically, use System.IO.FileOptions.DeleteOnClose. See similar question at Windows temporary files behaviour - are they deleted by the system?. A piece of sample code is like this
var path = Path.GetTempFileName();
Console.WriteLine(path);
using (var fs = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite, 4000,
FileOptions.DeleteOnClose))
{
fs.WriteByte(65);
}
I know it's silly to include other unrelated parameters, but it's how this API designed. You can wrap fs
in other steam or pass it to XDocument/XElement.Save()
var e = new XElement("Node");
e.Save(fs);