1

C# と VS 2012 Express を使用して Windows サービスを作成しました。サービスを正常にインストールした後、サービスを開始しようとすると、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);
    }
}
4

2 に答える 2

3

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;
 .....
于 2013-10-15T09:27:45.493 に答える
3

この線

this.ServiceName = MyServiceName;

OnStart() メソッドで問題が発生しています。

于 2013-10-15T09:27:54.293 に答える