2011-06-23 33 views
3

我最近「轉換」了或者將我的方法導入默認的Windows服務模板。沒有語法錯誤並且其編譯正常,但FileSystemWatcher方法由於某種原因不起作用,例如,當它正常運行時,它會將所有已創建的進程寫入process.lst,但是作爲服務運行時它不會這樣做(可能與工作目錄有關,因爲它的服務?):將C#控制檯應用程序轉換爲服務(主要方法不起作用)

namespace WindowsService 
{ 
    class WindowsService : ServiceBase 
    { 
     /// <summary> 
     /// Public Constructor for WindowsService. 
     /// - Put all of your Initialization code here. 
     /// </summary> 
     public WindowsService() 
     { 
      this.ServiceName = "My Service"; 
      this.EventLog.Source = "My Service"; 
      this.EventLog.Log = "Application"; 

      // These Flags set whether or not to handle that specific 
      // type of event. Set to true if you need it, false otherwise. 
      this.CanHandlePowerEvent = true; 
      this.CanHandleSessionChangeEvent = true; 
      this.CanPauseAndContinue = true; 
      this.CanShutdown = true; 
      this.CanStop = true; 

      if (!EventLog.SourceExists("My Service")) 
       EventLog.CreateEventSource("My Service", "Application"); 
     } 

     /// <summary> 
     /// The Main Thread: This is where your Service is Run. 
     /// </summary> 
     static void Main() 
     { 
      ServiceBase.Run(new WindowsService()); 

      // This checks for any existing running instances, if found the proess is terminated immidieately. 
      if (System.Diagnostics.Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetEntryAssembly().Location)).Count() > 1) return; 

      DisplayInfo(); 

      string dirPath = "C:\\"; 
      FileSystemWatcher fileWatcher = new FileSystemWatcher(dirPath); 
      fileWatcher.IncludeSubdirectories = true; 
      fileWatcher.Filter = "*.exe"; 
      // fileWatcher.Filter = "C:\\$Recycle.Bin"; 
      // fileWatcher.Changed += new FileSystemEventHandler(FileWatcher_Changed); 
      fileWatcher.Created += new FileSystemEventHandler(FileWatcher_Created); 
      // fileWatcher.Deleted += new FileSystemEventHandler(FileWatcher_Deleted); 
      // fileWatcher.Renamed += new RenamedEventHandler(FileWatcher_Renamed);  
      fileWatcher.EnableRaisingEvents = true; 
      // updated code 

      while (true) 
      { 
       CleanUpDel(); 

       StartRemoveDuplicate(); 

       CompareFiles(); 

       bool changes = ScanFileChanges(); 

       if (!changes) 
       { 
        TrimColon("process_trim.lst", "process_trimmed.lst"); 

        TrimWipe(); 

        AddTMPIgnore(); 

        SendAlert(); 

        CompareOrig(); 


       } 
       Thread.Sleep(10000); 
      } 
     } 


     private static void AddTMPIgnore() 
     { 
      var myString = File.ReadAllText("process_final.lst"); 
      File.AppendAllText("ignore_temp.lst", myString); 
     } 



     static void FileWatcher_Created(object sender, FileSystemEventArgs e) 
     { 

      using (StreamWriter fileWriter = new StreamWriter("process.lst", true)) 
      { 
       var data = true; 
       fileWriter.Write("C:\\" + e.Name + Environment.NewLine); 
      } 


     } 

回答

8

這是相當長一段時間我做了最後的服務,所以我只記得含糊,但:

有一個OnStart和調用OnStop方法。在此之內,你必須創建一個新的線程來完成這項工作。您可以使用BackgroundWorker或創建一個System.Threading.Thread。 當我正確解釋你的代碼時,你在Main方法中進行處理。這是不允許的。該服務將無法正確初始化。構造函數不是做這件事的地方。
請確保,如果OnStop被調用,您的處理邏輯確實停止。否則服務控制管理器不會喜歡你的服務。

+0

先前有關「NOOP循環如果無所事」的評論不正確。 – skb

0

您的服務可能沒有寫入文件的權限,或者將文件放置在您不期望的位置。

相關問題