2013-10-15 106 views
1

我使用C#創建了Windows服務#& VS 2012 Express。成功安裝服務後,當我嘗試啓動它時,我在Windows應用程序日誌中收到以下錯誤:服務運行時無法更改服務名稱。C#中的Windows服務無法啓動

我想我的手在C#VB6的編碼器(叫我瘋了針對與業務開始)...

您輸入將不勝感激。

這裏是我的代碼:

using System; 
using System.IO; 
using System.ComponentModel; 
using System.ServiceProcess; 
using System.Configuration.Install; 

public class KFolderWatcher : ServiceBase 
{ 
    public const string MyServiceName = "KFolderWatcher"; 
    private FileSystemWatcher watcher = null; 

    public KFolderWatcher() 
    { 
     this.ServiceName = "KFolderWatcher"; 
     this.CanStop = true; 
     this.CanPauseAndContinue = false; 
     this.AutoLog = true; 
    } 

    protected override void OnStart(string[] args) 
    { 
     this.ServiceName = MyServiceName; 

     // Create a new FileSystemWatcher with the path and jpg file filter 
     FileSystemWatcher watcher = new FileSystemWatcher("c:\\scans\\", "*.jpg"); 

     //Watch for changes in LastAccess and LastWrite times, and the renaming of   files or directories. 
     watcher.NotifyFilter = NotifyFilters.LastAccess 
         | NotifyFilters.LastWrite 
         | NotifyFilters.FileName 
         | NotifyFilters.DirectoryName; 

     // Add event handlers. 
     watcher.Changed += new FileSystemEventHandler(OnChanged); 
     watcher.Created += new FileSystemEventHandler(OnChanged); 
     watcher.Deleted += new FileSystemEventHandler(OnChanged); 
     watcher.Renamed += new RenamedEventHandler(OnRenamed); 

     // Begin watching. 
     watcher.EnableRaisingEvents = true; 
    } 

    protected override void OnStop() 
    { 
     watcher.EnableRaisingEvents = false; 
     watcher.Dispose(); 

     //LogEvent("Monitoring Stopped"); 
     Console.WriteLine("Monitoring Stopped"); 
    } 

    void OnRenamed(object sender, RenamedEventArgs e) 
    { 
     string log = string.Format("{0} | Renamed from {1}", e.FullPath, e.OldName); 
     //LogEvent(log); 
     Console.WriteLine(log); 
    } 

    public static void Main() 
    { 
     ServiceBase.Run(new KFolderWatcher()); 
    } 

    //This method is called when a file is created, changed, or deleted. 
    private static void OnChanged(object source, FileSystemEventArgs e) 
    { 
     //Show that a file has been created, changed, or deleted. 
     WatcherChangeTypes wct = e.ChangeType; 
     Console.WriteLine("File {0} {1}", e.FullPath, wct.ToString()); 
    } 

    protected void FileCreated(object sender, FileSystemEventArgs e) 
    { 
     if (e.ChangeType == WatcherChangeTypes.Created) 
     { 
      if (Directory.Exists(e.FullPath)) 
       Console.WriteLine("A new folder has been created."); 
      else 
       Console.WriteLine("A new file has been created."); 
     } 
    } 

} 

[RunInstaller(true)] 
public class KFolderWatcherInstaller : Installer 
{ 
    private ServiceProcessInstaller processInstaller; 
    private ServiceInstaller serviceInstaller; 

    public KFolderWatcherInstaller() 
    { 
     processInstaller = new ServiceProcessInstaller(); 
     serviceInstaller = new ServiceInstaller(); 
     processInstaller.Account = ServiceAccount.LocalSystem; 
     processInstaller.Username = null; 
     processInstaller.Password = null; 
     serviceInstaller.StartType = ServiceStartMode.Manual; 
     serviceInstaller.ServiceName = "KFolderWatcher"; 
     serviceInstaller.DisplayName = "Kempston Folder Watcher"; 
     serviceInstaller.Description = "Monitor Branch Folder for images to upload."; 
     Installers.Add(serviceInstaller); 
     Installers.Add(processInstaller); 
    } 
} 

回答

3

this.ServiceName = MyServiceName; 
在的OnStart()方法

引起問題。

+0

優秀!謝謝,我已經評論過這一行,並且它沒有問題。 – robhob

+0

對我來說,它是在設計器類的InitializeComponent() –

3

請避免在分配服務類的服務名稱。由於您已經創建了一個分配服務名稱的安裝程序,因此不需要將其寫入服務中。

serviceInstaller.ServiceName = "KFolderWatcher"; 

public class KFolderWatcher : ServiceBase 
{ 
    //public const string MyServiceName = "KFolderWatcher"; 
    private FileSystemWatcher watcher = null; 

    public KFolderWatcher() 
    { 
     // this.ServiceName = "KFolderWatcher"; 
     this.CanStop = true; 
     this.CanPauseAndContinue = false; 
     this.AutoLog = true; 
    } 

    protected override void OnStart(string[] args) 
    { 
     // this.ServiceName = MyServiceName; 
..... 
+0

實際上在設計器中還有一個屬性,您可以在其中更改服務名稱。你應該使用這個,而不是在代碼中做! –

+0

你是對的。 –

+0

謝謝,我正在快速版中執行此操作。是否支持按照您的建議更改設計師的姓名? – robhob