My C# code may be running inside an MVC3 application under IIS (currently 7.5, but I’d like to not depend on a specific version) or elsewhere.
Looks like one way to know that code is running under IIS is to check the current process name, but this approach depends on having a filename string hardcoded.
Is there some programmatic way to detect my code is running under IIS not depending on IIS version?
Have a look at the HostingEnvironment class, especially the IsHosted method.
This will tell you if you are being hosted in an ApplicationManager, which will tell you if you are being hosted by ASP.NET.
Strictly, it will not tell you that you are running under IIS but I think this actually meets your needs better.
Example code: