2016-02-29 88 views
0

运行。我知道,Windows服务无法写入到控制台,但不应该,因为我绕过ServiceBase.Run它只是运行作为一个正常的控制台应用程序?Windows服务参数为控制台应用程序

using System; 
using System.Collections.Generic; 
using System.Configuration.Install; 
using System.Linq; 
using System.Reflection; 
using System.ServiceProcess; 
using System.Text; 
using System.Threading.Tasks; 
using System.Diagnostics; 

namespace ShowCheckerService 
{ 
    static class Program 
    { 
     /// <summary> 
     /// The main entry point for the application. 
     /// </summary> 
     static void Main(params string[] args) 
     { 



    #if DEBUG 
      Service1 myService = new Service1(); 
      myService.OnDebug(); 
      System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite); 

    #else 
      if (Environment.UserInteractive) 
      { 
       string parameter = string.Concat(args); 
       switch (parameter) 
       { 
        case "--install": 
         ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location }); 
         break; 
        case "--uninstall": 
         ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location }); 
         break; 
        case "--console": 
         ConsoleMode(); 
         break; 
       } 
      } 

      else 
      { 

       ServiceBase[] ServicesToRun; 
       ServicesToRun = new ServiceBase[] 
       { 
       new Service1() 
       }; 
       ServiceBase.Run(ServicesToRun); 
      } 
    #endif 

     } 


     private static void ConsoleMode() 
     { 
      System.IO.File.Create(@"C:\ProgramData\ShowChecker\console.txt"); 
      Console.WriteLine("asdf");    
     } 


    } 
} 
+2

“输出型”的价值,你有什么想法? –

+0

@DmitryRazumikhin没错!我已将它设置为Windows应用程序。将其更改为控制台并立即输出。 – Jrow

回答

1

曾在属性来更改此:

enter image description here

+1

是的,这是一个正确的选择。 –

相关问题