2011-08-18 18 views
8

UPDATE からバッチファイルを実行しています。私はそれは、バッチファイルを実行しませんはまだ私は私のWindowsサービスに次のコードを持って正しい答え** を探していて、私はバッチファイルを実行したい** C#

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(); 
      } 


     } 
    } 

コマンドプロンプトウィンドウをアップしたいので、私はここに進行

を見ることができます私のコードが、私のバッチファイルのコードdoesntの作業が

ある 。私はネットとC#の初心者です。私はここから何をすべきか分かりません。私は

+0

は、どのような方法で、あなたはサービスが実行されていることを確認することはできますか? –

+0

サービスは正常に動作しています。私はコマンドラインから起動することができます。フォルダの名前を変更/削除/追加して情報を記録できます。だから私は100%サービスが正常に動作していることを確信しています – Autolycus

+3

サイドノートと同様に 'StreamWriter'を' using'にラップする必要があります。 – ChrisWue

答えて

4

How to run console application from Windows Service?

あなたが設定ファイル名= "cmd.exeの" と引数を持つp.StartInfo = "\\ thebatfile.batはC" になるでしょうサービスが最初に実行されたときにバッチスクリプトが実行され、他の機能が呼び出される前に終了します(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

try..doesnt workどちらでも – Autolycus

+3

@Autolycusはい、そうですが、引数を '/ C" c:\ myFile.bat "'( '/ C'が欠けています)に変更する必要があります。 – Justin

0

を信じてそれは次のようになります。 感謝

0

私はあなたのサービスまたはbatファイルを疑っています。ソースコードを変更してメモ帳を開きます!メモ帳が表示されるかどうか確認してください!はいの場合は、さらに調査することができます!

+2

私はあなたがWindowsサービスでメモ帳を開始できるとは思わない。 – Autolycus

+0

私はそれがうまくいくはずだと思って、Windowsサービスでメモ帳を取得するために余分な作業が必要であることを知りませんでした。それ以前に試してみる理由はありませんでした! http://social.msdn.microsoft.com/Forums/en/netfxbcl/thread/c8c9d2c6-f2bb-4fa4-ba99-51a42f9bc6f4 – ioWint

0

バッチファイルは何をしていますか?これがOKであることを確認したとします。

+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で開く必要があることがわかります。これを無効にしても、これらのことは分かりません。何かを正常に実行するには、実行ファイルを渡す必要があります。

あなたが代わりにcmd.exeFileNameを設定することにより、cmd.exeを経由してバッチファイルを実行し、/C "c:\myFile.bat"への引数する必要がtrueにRedirectStandardOutputを設定していると:desktoplessのユーザーアカウントで実行

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

大きな説明!ありがとう! – Tyler

0

Windowsサービス。 cmdウィンドウを表示するには、現在ログインしているユーザーを偽装し、このユーザーのデスクトップでcmdウィンドウを開始する必要があります。これを参照してください。

Windows Impersonation from C#

関連する問題