How to determine if starting inside a windows service?

前端 未结 6 808
半阙折子戏
半阙折子戏 2021-02-15 16:37

Currently I\'m checking it in the following way:

if (Environment.UserInteractive)
    Application.Run(new ServiceControllerForm(service));
else
    ServiceBase.R         


        
6条回答
  •  面向向阳花
    2021-02-15 17:19

    Why not just use a command line switch?

    // Note that you have to add the params argument, 
    // which isn't usually present in windows services
    private static void Main(params string[] parameters)
    {
        ....
    
        if (parameters.Length > 0)
        {
            if (parameters[0].ToLower() == "/console")
            {
                Application.Run(new ServiceControllerForm(service));  
            {
            else
            {
                ServiceBase.Run(windowsService);
            }
        }
    }
    

提交回复
热议问题