Heinnge
Heinnge

Reputation: 858

am i in console app?

is there a way to detect inside the component in C# if the application that uses this component is running in console application or others?

Upvotes: 2

Views: 168

Answers (4)

Ran
Ran

Reputation: 6159

The information you need is a property of your exe assembly. If you use a tool such as ildasm.exe to view your assembly's manifest, you can see the .subsystem property that indicates the type of execution environment that launches your Main method.

However, it seems like Reflection doesn't expose this info directly.

The code in the following link contains an example for how to read the .exe file itself and detect this infromation:

http://blogs.msdn.com/b/kstanton/archive/2004/03/31/105060.aspx

I hope this helped.

Upvotes: 1

Otávio Décio
Otávio Décio

Reputation: 74280

Use Environment.UserInteractive. If true you are in a console. If false you could be in a service.

Upvotes: 8

Kris Krause
Kris Krause

Reputation: 7326

Reflection is one of the first places I would look -

http://msdn.microsoft.com/en-us/library/system.reflection.assembly.getexecutingassembly.aspx

Q: How "sure" do you need to be?

Upvotes: 0

Benny
Benny

Reputation: 3917

Please see this example for your solution: http://weblogs.asp.net/whaggard/archive/2004/08/30/223020.aspx

Upvotes: 1

Related Questions