1

C# サービスを開始しようとすると、「starting」と表示され、「stopped」に戻ります。何が問題なのですか? 以前は同じコードを使用していましたが、機能しましたが、コードにいくつかの変更を加えたため、機能しなくなりました。コードに追加したものは次のとおりです。

アプリ構成:

<add key="cut-copy" value="copy"/>

通常のコード:

    private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
{
    String cut_copy = ConfigurationManager.AppSettings[@"cut-copy"];

if (cut_copy == "copy")
    {
        cut = false;
    }
    else
    {
        cut = true;
    }
if (WaitForFileAvailable(e.FullPath, TimeSpan.FromSeconds(10)))
            {
            var file = Path.Combine(source, e.Name);
            var copy_file = Path.Combine(target, e.Name);
            var destination = Path.Combine(target, Path.ChangeExtension(source, Path.GetExtension(source)));

                    if (File.Exists(file))// Check to see if the file exists. 
                    {                     //If it does delete the file in the target and copy the one from the source to the target.
                        File.Delete(copy_file);
                        File.Copy(e.FullPath, Path.Combine(target, e.Name));
                    }
                    else// If it doesn't, just copy the file.
                    {
                        if (cut == true)
                        {
                            if (File.Exists(file))// Check to see if the file exists. 
                            {                     //If it does delete the file in the target and copy the one from the source to the target.
                                File.Delete(copy_file);
                                File.Move(Path.Combine(e.FullPath, e.Name), target);
                            }
                        }
                        else
                        {
                            if (File.Exists(file))// Check to see if the file exists. 
                            {                     //If it does delete the file in the target and copy the one from the source to the target.
                                File.Delete(copy_file);
                                File.Copy(e.FullPath, Path.Combine(target, e.Name));
                            }
                        }
    //under this is more code that didn't change
    }

編集:開始:

protected override void OnStart(string[] args)
    {
      base.OnStart(args);
      this.fileSystemWatcher1 = new System.IO.FileSystemWatcher();
      ((System.ComponentModel.ISupportInitialize)(this.fileSystemWatcher1)).BeginInit();
      fileSystemWatcher1.Path = source;
      fileSystemWatcher1.Changed += new FileSystemEventHandler(fileSystemWatcher1_Changed);
      fileSystemWatcher1.Created += new FileSystemEventHandler(fileSystemWatcher1_Created);
      fileSystemWatcher1.Deleted += new FileSystemEventHandler(fileSystemWatcher1_Deleted);
      fileSystemWatcher1.Renamed += new RenamedEventHandler(fileSystemWatcher1_Renamed);

      this.fileSystemWatcher1.EnableRaisingEvents = true;
      this.fileSystemWatcher1.IncludeSubdirectories = true;
      ((System.ComponentModel.ISupportInitialize)(this.fileSystemWatcher1)).EndInit();
      logger("Service started "+ DateTime.Now);
    }

私は何を間違っていますか?

4

2 に答える 2