- 1 year ago
How to determine whether an app is WinForms/WPF and not a console or asp.net or something else?
To determine whether an app is WinForms or WPF and not a console, ASP.NET, or something else, you can inspect the application's assembly using reflection. Here's how:
- Load the application's assembly using
Assembly.LoadFrom()
method:
csharpAssembly assembly = Assembly.LoadFrom("path/to/your/application.exe");
- Loop through all types in the assembly and check if they inherit from
System.Windows.Forms.Form
orSystem.Windows.Window
:
csharpforeach (Type type in assembly.GetTypes())
{
if (type.IsSubclassOf(typeof(System.Windows.Forms.Form)))
{
// The application is a WinForms app
}
else if (type.IsSubclassOf(typeof(System.Windows.Window)))
{
// The application is a WPF app
}
}
This code snippet loads the assembly of the application specified by the file path and loops through all the types in the assembly. If a type is a subclass of System.Windows.Forms.Form
, it indicates that the application is a WinForms app. If a type is a subclass of System.Windows.Window
, it indicates that the application is a WPF app.
Note that this approach assumes that the main form/window of the application inherits from the WinForms or WPF form/window classes. If the main form/window is implemented differently, this approach may not work.
Additionally, this approach requires that the application's assembly is available and can be loaded by your code. If the application is a compiled executable without any accompanying DLLs, you can still use this approach by temporarily copying the executable to a location where your code can access it, loading the assembly, and then deleting the temporary copy.