Currently I have an assembly used for a windows UI application which got a lot of logic built in and incase of errors it shows the messages to user with an ShowError() Method which internally uses Messagebox.Show(..) and then log the errors to a log file/database.
And here is my issue. I want to use the same assembly for a windows service. But when I use this assembly in a Windows service it will crash on Messagebox.show(..) because windows service will not allow UI interaction in normal mode(I know the option “Allow service to interact with Desktop”, but it is not an option for me).
So what I want to do is to do some thing like this.
if(!IsWindowsService())
MessageBox.Show("Message");
Logger.Log("Message");
Here IsWindowsService() should return true if the Assembly is being used by a Windows service and false if used by a Windows Application.
Any idea how to get this?
Have a look at the Environment.UserInteractive property. It should tell you exactly what you want.