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

后端 未结 4 1218
执念已碎
执念已碎 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:22

    Try checking Application.MessageLoop. It should be true for Windows Forms applications (that have a WinForms message loop), and false for windows services. I don't know what it would return for ASP.NET.

    As for console applications, they would have no message loop so they would return false. You can check for that using most properties in the Console class, but I warn you that it's a HACK. If you must, I'd go with:

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

    Note, that a console app could still call Console.SetIn(StreamReader.Null) or a windows app could call Console.SetIn(something else), so this is easily tricked.

提交回复
热议问题