tags:

views:

76

answers:

4

Well
this is a really simple question, the search tearms are just not that great.

How do I check in some library if I am currently running as a console application, vs. a WPF window application?

Thanks for any tips,

Chris

+1  A: 

You can check whether the current thread is a WPF UI thread by checking Dispatcher.Current.

SLaks
A: 

ILDasm will have an entry in the manifest as follows :

.subsystem 0x0003       // WINDOWS_CUI

.subsystem 0x0002       // WINDOWS_GUI

based on the subsystemtype you can tell if its GUI or CUI.

This information is also available from the following command :

dumpbin ConsoleApplication1.exe /headers

From your library query for entry assembly and get its full path(Assembly.GetEntryAssembly().CodeBase) and then you can issue any of these command to know the subsystem.

Nitin Chaudhari
+2  A: 

There's more, what if your library method is called from a worker thread? You didn't tell why you need to know, preventing a good answer. One approach is that the app that uses your library never has any trouble knowing whether its console or WPF. Expose a property to allow it to tell you. Another is using events so the app can simply implement the event handler to its liking. Dependency Injection is another.

Hans Passant
A: 

You can check if the executed statements are running in a WPF host with the following statement:

if (System.Windows.Application.Current != null)
{
//statements for WPF mode
}
else
{
//statements for non WPF mode...
}  

For this you must reference PresentationFramework.dll

csizo