Develop C # application type, the class contained in the DLL is used

Is there any way to find out in C # the type of running application.

Windows Service ASP.NET Windows Form Console

I would like to respond to the type of application, but cannot find a way to determine it.

+3
source share
4 answers

You must have a client code to tell your code what the context is, and then work with it. At best, you can guess based on external factors.

If you have to guess, this is what I would be looking for:

  • For ASP.NET, I would look for HttpContext.Current
  • For Windows Forms, I see if there are all elements in the static OpenForms collection of the Application class.
  • Windows Presentation Foundation , Application .
  • , , ..
  • , , , .
+5

Application.MessageLoop. Windows Forms ( WinForms) false Windows. , ASP.NET.

, , false. , Console, , HACK. , :

bool isConsole = Console.In != StreamReader.Null;

, Console.SetIn(StreamReader.Null) Windows Console.SetIn(something else), .

+2

ASP.NET, check if HttpContext.Current is null

+2
source

To test a Forms, WPF, WCF application or console:

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;
0
source

Source: https://habr.com/ru/post/1703472/


All Articles