Path.GetTempFileName()


In C# the class Path gives an easy way to get a temporary file for your application but you have to be careful using this API. This API create zero-byte temporary files in Windows temporary folder and when there are 65536 files it gives an IOException saying "The file exists.". But if you need small number of temporary files for short period of time then this API gives you a very easy way of creating temporary file all you need to do is to keep cleaning temporary files you created on regular basis.

Comments (2)

  1. Anonymous says:

    Much more unproblematic is to create a new GUID (Guid.NewGuid()) and include it in the temporary file name. You can be sure that it doesn’t already exist.

  2. MSDN Archive says:

    Well its easy. but i would like to know if its so good why is it not used as standard. My take would be that this would have significant perf hit if you are using too many temp files.

    What Path.GetTempFileName() does is it takes the name temp****.*** and tries different permutations of allowed NTFS characters in place of *’s. it fails when it runs out of names. its so much more pain to do than simple call for new GUID. There has to be a reason why they choose it over GUID. correct me if i am wrong.

Skip to main content