2011-08-18 14 views
8

UPDATE 運行批處理文件**仍在尋找正確的答案** 我有下面的代碼在我的Windows服務,我要運行一個批處理文件。我想在命令提示符窗口,這樣我就可以看到進步從C#

這裏是我的代碼,但我的批處理文件代碼does not工作

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Diagnostics; 
using System.Linq; 
using System.ServiceProcess; 
using System.Text; 
using System.IO; 

    namespace Watcher 
    { 
     public partial class Watcher : ServiceBase 
     { 
      public Watcher() 
      { 
       InitializeComponent(); 
      FolderWatcher.Created += FolderWatcher_Created; 
      FolderWatcher.Deleted += FolderWatcher_Deleted; 
      FolderWatcher.Renamed += FolderWatcher_Renamed; 
      } 

      protected override void OnStart(string[] args) 
      { 

          // Start the child process. 
      Process p = new Process(); 
      // Redirect the output stream of the child process. 
      p.StartInfo.UseShellExecute = false; 
      p.StartInfo.RedirectStandardOutput = true; 
      p.StartInfo.FileName = "C:\\myFile.bat"; 
      p.Start(); 
      // Do not wait for the child process to exit before 
      // reading to the end of its redirected stream. 
      // p.WaitForExit(); 
      // Read the output stream first and then wait. 
      string output = p.StandardOutput.ReadToEnd(); 
      p.WaitForExit(); 


      } 

      protected override void OnStop() 
      { 
      } 

      private void FolderWatcher_Created(object sender, System.IO.FileSystemEventArgs e) 
      { 
       TextWriter writer = new StreamWriter("C:\\folder\\FolderLog.txt", true); 
       writer.WriteLine(DateTime.Now + " A new folder/file with name " + e.Name + " has been created. "); 
       writer.Close(); 
      } 

      private void FolderWatcher_Deleted(object sender, System.IO.FileSystemEventArgs e) 
      { 
       TextWriter writer = new StreamWriter("C:\\folder\\FolderLog.txt", true); 
       writer.WriteLine(DateTime.Now + " A new folder/file with name " + e.Name + " has been deleted. "); 
       writer.Close(); 
      } 

      private void FolderWatcher_Renamed(object sender, System.IO.RenamedEventArgs e) 
      { 
       TextWriter writer = new StreamWriter("C:\\folder\\log.txt", true); 
       writer.WriteLine(DateTime.Now + " A new folder/file with name " + e.Name + " has been renamed. "); 
       writer.Close(); 
      } 


     } 
    } 

它不執行批處理文件。我是.net和C#的新手,我不確定在這裏做什麼。 感謝

+0

你能以任何方式確認服務正在運行? –

+0

服務運行得很好。我可以從命令行啓動它。我可以重命名/刪除/添加文件夾,並記錄信息。所以我100%肯定服務運行正常 – Autolycus

+3

正如旁註:你應該將'StreamWriter'封裝在'using'中,否則你可能會離開文件句柄。 – ChrisWue

回答

0

它看起來像它在服務第一次運行時運行批處理腳本,然後在其他函數被調用之前退出(p.WaitForExit();)。這是預期的行爲?這可以解釋爲什麼你可以看到它執行文件夾操作並且看不到正在運行的腳本。

試試這個代碼來調出控制檯窗口。它應該讓你知道批處理腳本何時運行。

protected override void OnStart(string[] args) 
{ 
     // Start the child process. 
     Process p = new Process(); 
     // Redirect the output stream of the child process. 
     p.StartInfo.UseShellExecute = false; 

     /* 
     This is commented out so we can see what the script is doing 
     inside the cmd console. 
     */ 
     //p.StartInfo.RedirectStandardOutput = true; 

     p.StartInfo.FileName = "C:\\myFile.bat"; 
     p.Start(); 
     // Do not wait for the child process to exit before 
     // reading to the end of its redirected stream. 
     // p.WaitForExit(); 
     // Read the output stream first and then wait. 

     /* 
     Since we aren't redirecting the output, we have to comment out 
     this line or we get an error 
     */ 
     //string output = p.StandardOutput.ReadToEnd(); 

     p.WaitForExit(); 
} 
0

我懷疑你的服務或bat文件。修改源代碼打開記事本!檢查記事本是否顯示!如果是的話,我們可以進一步調查!

+2

我不認爲你可以在Windows服務中啓動記事本。 – Autolycus

+0

我認爲它應該工作,不知道它需要額外的工作,以獲得記事本上的Windows服務。從來沒有理由早點嘗試! http://social.msdn.microsoft.com/Forums/en/netfxbcl/thread/c8c9d2c6-f2bb-4fa4-ba99-51a42f9bc6f4 – ioWint

0

你的批處理文件在做什麼?假設你已經確認這個IS運行正常。

+0

作爲一個附註 - 爲什麼你想從Windows服務中運行GUI?似乎很奇怪。然而,你可能會發現這個答案和一些使用文章:http://stackoverflow.com/questions/677874/starting-a-process-with-credentials-from-a-windows-service – glendon

2

問題是您的UseShellExecute爲false,但您未傳遞可執行文件的名稱。

當使用ShellExecute類似於雙擊瀏覽器中的文件 - 它知道.doc文件需要用Word打開,該.bat文件需要打開cmd.exe。當你有這個禁用,但它不知道這些東西,你需要通過一個可執行文件爲了任何東西成功運行。

當你設置RedirectStandardOutput爲true,則需要通過設置FileNamecmd.exe和參數通過cmd.exe去運行該批處理文件/C "c:\myFile.bat"

p.StartInfo.FileName = "cmd.exe"; 
p.StartInfo.Arguments = "/C \"c:\\myFile.bat\""; 
+0

很好的解釋!謝謝! – Tyler

0

Windows服務下desktopless用戶帳戶運行。要查看cmd窗口,您必須模擬當前記錄的用戶並在該用戶的桌面上啓動cmd窗口。看到這個:

Windows Impersonation from C#