If this is happening to you on a production environment or with an app that you can’t change, the quick fix is to empty the Temp folder.
Depending on the user that is running the application you should either
- Empty
C:\Windows\Temp(for IIS or services running underLocalSystemaccount) - Or
%temp%for locally logged on users (which for me isC:\Users\MyUserName\AppData\Local\Temp).
On the other side, if your own code is throwing this, and you want to prevent this from happening ever again:
- Do not use System.IO.Path.GetTempFileName()!
GetTempFileName() is a wrapper of the two decades old Win32 Api. It generate file names that will very easily collide. It circumvents those collitions by heavily looping on the file system, iterating possible file names from "%temp%\tmp0000.tmp" to "tmpFFFF.tmp" and skipping already existing ones. This is a I/O intensive, slow, and frankly terrible algorithm. Also using only 4 hex characters is what makes the artificial limit of 65536 files before failing.
The alternative is to generate file names that will not collide. For example, lets reuse GUID's logic: 32 hex digits will almost never collide.
private string GetTempFileName()
{
return Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
}
// Sample: c:\Windows\Temp\2e38fe87-f6bb-4b0d-90b3-2d07016324c1
This expands the limit from 65k to 4k millions files max (theoretically)… Of course, having leaked 65k files is already terrible, so…
- Do not leak temp files!
Double check your app for all happy and unhappy paths (like unexpected exceptions). Ensure it’s correctly disposing each FileStream and deleting the temp files in Finally blocks .
- Clean the temp folder
Clean it now, and educate the system administrator to clean it periodically, because you can’t trust every app in the wild.
On my own servers I would automate this task using:
- For global Windows\Temp
schtasks /Create /TR "cmd /c call DEL /F /S /Q %^TEMP%" /TN "Delete Global Temp Files" /sc WEEKLY /ST 12:00 /ru system
- For current user:
schtasks /Create /TR "cmd /c call DEL /F /S /Q %^TEMP%" /TN "Delete %username% Temp Files" /sc WEEKLY /ST 12:00