Friday, February 29, 2008

How to check if class has been used in Web or Windows application

After a while, I decided to participate in a newsgroups tonight and found an interesting question. "Is there a programmatic way for the class lib to determine what kind of app is running - web or windows". The worst, guy requested official response from Microsoft support and didn't get an answer. After 3 days his question wasn't answered, he re-posted it. So, is there are something wrong? Looks like not.
The simple answer from my side was, to implement following function somewhere in his shared assembly:
public bool IsWebApp(){
return (HttpContext.Current != null);

So, if there is Current HttpContext than function has been called from ASP.NET application and if there is not such an object, function is called from Windows Forms application.
Simple enough.

No comments: