简体   繁体   English

C#中的Windows服务无法启动

[英]Windows Service in C# Won't Start

I've created a Windows Service using C# & VS 2012 Express. 我已经使用C#和VS 2012 Express创建了Windows服务。 After I've successfully installed the service but when I try to start it I get the following error in the Windows Application Log: Cannot change service name when the service is running. 成功安装服务后,当我尝试启动该服务时,Windows应用程序日志中出现以下错误:服务运行时无法更改服务名称。

I'm a VB6 coder trying my hand at C# (call me crazy for starting with a service)... 我是一位尝试使用C#的VB6编码器(因开始使用服务而感到疯狂)...

You input would be greatly appreciated. 您的输入将不胜感激。

Here's my code: 这是我的代码:

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

Please avoid assigning the service name in Service class. 请避免在“服务”类中分配服务名称。 Since you have already created an installer which assigns the service Name, it is not required to write it inside service. 由于您已经创建了分配服务名称的安装程序,因此不需要将其写入服务中。

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;
 .....

The line 线

this.ServiceName = MyServiceName;

in the OnStart() method is causing the problem. 在OnStart()方法中导致此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM