2016-02-29 94 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

是的,這是一個正確的選擇。 –

相關問題