Daily Knowledge Drop
Sometimes an application needs to create a temporary file to store some data - .NET has built-in functionality to create temporary files, using the Path.GetTempFileName
method.
This method will create a uniquely named, zero-byte temporary file on disk and returns the full path of that file.
GetTempFileName
Creating a temporary file is incredibly simple using the GetTempFileName
method:
var temporaryFile = Path.GetTempFileName();
Console.WriteLine(temporaryFile);
This creates a temporary file in the users temporary folder - below is the output on Windows and on a Linux container respectively:
C:\Users\username\AppData\Local\Temp\tmp6F69.tmp
/tmp/tmpZZGMrL.tmp
GetTempPath
If you would like to specify the name and extension of the temporary file explicitly, the GetTempPath
method can be used to get the temporary folder location, then have the name and extension specified:
string tempTxtFile = Path.GetTempPath() + Guid.NewGuid().ToString() + ".txt";
Console.WriteLine(tempTxtFile);
This creates a temporary file in the users temporary folder with the given name and extension - below is the output on Windows and on a Linux container respectively:
C:\Users\username\AppData\Local\Temp\b3eeca7e-e965-4794-9ffe-1fada639689d.txt
/tmp/c79a3579-42fc-49e7-a329-42d6bc053ada.txt
Deletion
Even though the files are called temporary files
, they are not temporary in existence: they are not automatically deleted or cleaned up. The temporary naming refers to the type of data it is designed to contain.
If the files are not manually cleaned up and deleted, it is possible that no unique name is available and a new temporary file could not be created - in which case an IOException
is thrown.
The recommendation would be to have the application delete the file either after use, or when the application shuts down.
Notes
Using the built in GetTempFileName
is definitely easier, quicker and simpler than trying to manually manage creating temporary files and the complications regarding file and folder permissions. The GetTempFileName
functionality should be the default go-to method for creating temporary files.
References
Daily Drop 77: 19-05-2022
At the start of 2022 I set myself the goal of learning one new coding related piece of knowledge a day.
It could be anything - some.NET / C# functionality I wasn't aware of, a design practice, a cool new coding technique, or just something I find interesting. It could be something I knew at one point but had forgotten, or something completely new, which I may or may never actually use.
The Daily Drop is a record of these pieces of knowledge - writing about and summarizing them helps re-enforce the information for myself, as well as potentially helps others learn something new as well.On This Page