John Spiegel John Spiegel - 2 months ago 19x
ASP.NET (C#) Question

How do I programmatically delete ASP.NET Temp Internet Files

First off, I'm not asking how to find the path or use the IO classes, etc.

The issue I have is often when restarting my site in development from Visual Studio (2010) (Win7), it's still clinging onto prior references to ASP.NET Temporary Internet Files, and I've found that once I delete them the problem goes away.

Manually deleting, I've found I must drill down multiple layers of subfolders and delete their contents before I can delete in the root folder.

I've tried to programmatically do the deletes with a little utility program, but get permissions errors (UnauthorizedAccessException), even when I run 1) from VS started as "Run as Administrator" or when kicking off my exe directly with "Run as Administrator".

Anyone have a smoother answer than manually drilling to subfolders?



This isn't a solution per se, though the reason you can't delete the files is because IIS or Cassini (the built-in Visual Studio web server) is putting a lock on them.

Kill the application pool running the application if you're using IIS, or close Visual Studio (or kill the ASP.NET server from its tray icon) if you're using Cassini; then you can delete the root folder in the Temporary ASP.NET Files directory.

Then just restart the application pool or Visual Studio (if you closed it) and you're back to work!

If you're using IIS and it's version 7 or greater, there's a command line utility you can run to stop it:

appcmd start apppool / DefaultAppPool

It would be easier to just whip up a batch script to do it for you:

@echo off
appcmd stop apppool / DefaultAppPool
del "C:\Windows\Microsoft.NET\Framework\\Temporary..." /f /q
appcmd start apppool / DefaultAppPool

If you're using IIS 6.0 then here's an answer with a PowerShell script to start and stop it. You could modify it to delete the temp folder while it's working.