2011-08-18 22 views
8

UPDATE ** Nadal szukasz poprawną odpowiedź ** Mam następujący kod w moim usługi windows i chcę uruchomić plik wsadowy. Chcę okno wiersza polecenia tak widzę postępyuruchomiony plik wsadowy z C#

tutaj jest mój kod, ale mój plik wsadowy pracom kod robi

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


     } 
    } 

To nie wykonuje plik wsadowy. Jestem nowicjuszem w .net i C# i nie jestem pewien, co robić dalej. dzięki

+0

można w żaden sposób potwierdzić, że usługa jest uruchomiona? –

+0

usługa działa dobrze. Mogę uruchomić go z linii poleceń. Mogę zmieniać nazwy/usuwać/dodawać foldery i loguje informacje. Tak więc jestem w 100% pewny, że usługa działa dobrze – Autolycus

+3

Tak samo jak notatka: powinieneś zawijać 'StreamWriter' 'in' używając 'w przeciwnym razie możesz pozostawić otwarte uchwyty plików. – ChrisWue

Odpowiedz

0

Wygląda na to, uruchamia skrypt wsadowy, gdy usługa jest uruchamiana po raz pierwszy, a następnie kończy pracę (p.WaitForExit();), zanim inne funkcje uzyskają możliwość wywoływania. Czy to zamierzone zachowanie? To by wyjaśniało, dlaczego można zobaczyć, że wykonuje operacje na folderach i nie widzi uruchamianego skryptu.

Spróbuj wpisać ten kod, aby wyświetlić okno konsoli. To powinno dać ci pojęcie, kiedy działa skrypt wsadowy.

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

Mam wątpliwości co do Twojej usługi lub pliku nietoperza. zmodyfikuj kod źródłowy, aby otworzyć notatnik! sprawdź, czy pojawia się notatnik! jeśli tak, możemy zbadać dalej!

+2

Nie sądzę, możesz uruchomić notatnik w systemie Windows. – Autolycus

+0

Zakładam, że to powinno działać, nie wiedziałem, że potrzebuje dodatkowej pracy, aby uzyskać notatnik na usługę Windows. nigdy nie miałem powodu, aby wypróbować go wcześniej! http://social.msdn.microsoft.com/Forums/en/netfxbcl/thread/c8c9d2c6-f2bb-4fa4-ba99-51a42f9bc6f4 – ioWint

0

Co robi twój plik wsadowy? Załóżmy, że potwierdziłeś, że to działa poprawnie.

+0

Na marginesie - dlaczego próbujesz uruchomić GUI z usługi Windows? Wydaje się dziwne. Jednak może się okazać, że ta odpowiedź i artykuł niektórych wykorzystania: http://stackoverflow.com/questions/677874/starting-a-process-with-credentials-from-a-windows-service – glendon

2

Problem polega na tym, że masz UseShellExecute jako false, ale nie przekazujesz nazwy pliku wykonywalnego.

Kiedy używa się ShellExecute, podobnie jak dwukrotne kliknięcie pliku w eksploratorze - wie, że pliki .doc muszą zostać otwarte w programie Word, a te pliki .bat muszą zostać otwarte przy pomocy cmd.exe. Kiedy masz to wyłączone, ale nie zna żadnej z tych rzeczy i musisz przekazać plik wykonywalny, aby wszystko działało poprawnie.

Jak ustawiania RedirectStandardOutput true trzeba zamiast uruchomić plik wsadowy poprzez cmd.exe ustawiając FileName do cmd.exe i argumenty do /C "c:\myFile.bat":

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

Doskonałe wyjaśnienie! Dzięki! – Tyler

0

Windows Services uruchomić na koncie użytkownika desktopless. Aby zobaczyć okno cmd, musisz podszyć się pod bieżącego zalogowanego użytkownika i uruchomić okno cmd na pulpicie tego użytkownika. Zobacz to:

Windows Impersonation from C#

Powiązane problemy