views:

97

answers:

1

How can I check at runtime whether a C# application is a Windows application or a console application?

I want to write a generic output library (output to textbox or console when console app).
For that reason, if I could check whether it is a asp application would be useful, too.

+6  A: 

I think you may be attacking the problem in the wrong way. Without knowing more about what you are doing here is what I'd suggest:

Create an interface called something like IOutputWriter with a Write(...) method. Then create an implementation for each environment (ConsoleOutputWriter, TextboxOutputWriter, etc...). Each environment can use the appropriate implementation since they know what type of app they are.

Daniel Auger
Yep, and such an system already exists inf the form of TraceListeners.
Henk Holterman
Yea, that's what I want to do. But as stated, my problem is how do I know the environment (console/win/web), so I can shift the pointer/delegate to use the appropriete implementation ?
Quandary
Use dependency injection at app start-up to configure output writer implementation. So instead of your output writer api trying to figure out the environment at runtime something external configures it to the proper type.If you can elaborate more on what exactly you are trying to do, I can try to give you more of a concrete answer.
Daniel Auger