0

I have a class library. How do I determine if it is running in a Windows Service versus something else? For example a console application, WPF, ASP.NET, WinForms?

Greg Finzer
  • 6,714
  • 21
  • 80
  • 125
  • 1
    It's usually better, if the calling "environment" matters, that you define an interface that your callers have to implement, that lets *them* choose how best to adapt to their "environment". That way, your code doesn't have to contain swiss army knives to deal with such factors. Of course, once you've defined such an interface, you're free to provide default implementations of the interface that are suitable for different "environments" - but leave it up to your callers to select the correct one (or choose their own implementation) – Damien_The_Unbeliever May 16 '13 at 12:34
  • What do you need this for? – Dennis May 16 '13 at 12:35
  • I just found this stack overflow question: http://stackoverflow.com/questions/200163/am-i-running-as-a-service – Greg Finzer May 16 '13 at 14:48
  • @Greg''Wildman''Finzer Just as a matter of interest, why does you class library need to know if it's running in a service? – Matthew Watson May 16 '13 at 17:30
  • Matt, Dennis, sorry, I cannot answer that. – Greg Finzer May 20 '13 at 18:09

1 Answers1

3

If you are trying to determine if you are allowed to display any user interface, use:

Environment.UserInteractive

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276