title: C# Windows Service won't start
body: <p>When I try to start my c# service it says:"starting" for a second and it turns back to being "stopped"
   What can be the problem? I had the same code before, and it worked but made some changes in the code now and it stopped working. Here is what I added to my code:</p>
  
  <p>App Config:</p>
  
  <pre><code>&lt;add key="cut-copy" value="copy"/&gt;
  </code></pre>
  
  <p>Normal code:</p>
  
  <pre><code>    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
      }
  </code></pre>
  
  <p>EDIT: <strong>ONSTART:</strong></p>
  
  <pre><code>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);
      }
  </code></pre>
  
  <p>What am I doing wrong?</p>