Work out the type of c# application a class contained in a DLL is being used by

后端 未结 4 1210
执念已碎
执念已碎 2021-01-06 21:33

Is there any way to know in C# the type of application that is running.

Windows Service ASP.NET Windows Form Console

I would like to react to the application

4条回答
  •  旧巷少年郎
    2021-01-06 22:13

    To check for a Forms, WPF, WCF or Console application:

    if (System.Windows.Forms.Application.OpenForms.Count > 0)
    {
        return ApplicationType.WindowsForms;
    }
    
    if (System.Windows.Application.Current != null)
    {
        return ApplicationType.Wpf;
    }
    
    if (System.ServiceModel.OperationContext.Current != null)
    {
        return ApplicationType.Wcf;
    }
    
    try
    {
        int windowHeight = Console.WindowHeight; // an exception could occur
        return ApplicationType.Console;
    }
    catch (IOException)
    {
    }
    
    return ApplicationType.Unknown;
    

提交回复
热议问题