title: Windows service on Local Computer started and then stopped error
body: <p>Usually, I get this error:
  (The "service name" service on Local Computer started and then stopped. Some services stop automatically if they are not in use by other service or programs) when there's something wrong with my code, like non-existing drive paths, etc. The windows service will not start.</p>
  
  <p>I have a windows service that backs up folder/files, to a location if it reached the size limit. Details are all provide by an XML Configuration that the windows service reads on start. I have a separate windows forms that has a button that does exactly what my windows service's onstart is doing. I use my windows forms for debugging the code before I put it in my windows service.</p>
  
  <p>When I start my windows forms. It does what it suppose to do. When I put my code in the windows service OnStart() method the error showed up.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>protected override void OnStart(string[] args)
  {
  
      private static string backupConfig = @"D:\LogBackupConfig\backupconfig.xml";
      private static string serviceStat = @"D:\LogBackupConfig\Status.txt";
      private static string fileFolderStat = @"D:\LogBackupConfig\FileFolderStat.txt";
  
      protected override void OnStart(string[] args)
      {
          if (File.Exists(backupConfig))
          {
              FileSystemWatcher watcher = new FileSystemWatcher();
              XmlTextReader reader = new XmlTextReader(backupConfig);
  
              XmlNodeType type;
              List&lt;string&gt; listFile = new List&lt;string&gt;();
              string fileWatch = "";
  
              //this loop is for reading XML elements and assigning to variables
              while (reader.Read())
              {
                  type = reader.NodeType;
                  if (type == XmlNodeType.Element)
                  {
                      if (reader.Name == "File")
                      {
                          reader.Read();
                          fileWatch = reader.Value;
                      }
                      else if (reader.Name == "Folder")
                      {
                          reader.Read();
                          fileWatch = reader.Value;
                      }
                  }
              }
              reader.Close();
  
              watcher.Path = fileWatch;
              watcher.Filter = "*.*";
  
              //this loop reads whether the service will watch a file/folder
              XmlTextReader reader1 = new XmlTextReader(backupConfig);
              while (reader1.Read())
              {
                  type = reader1.NodeType;
                  if (type == XmlNodeType.Element)
                  {
                      if (reader1.Name == "File")
                      {
                          watcher.IncludeSubdirectories = false;
                          watcher.Changed += new FileSystemEventHandler(OnChangedFile);
                      }
                      else if (reader1.Name == "Folder")
                      {
                          watcher.IncludeSubdirectories = true;
                          watcher.Changed += new FileSystemEventHandler(OnChangedFolder);
                      }
                  }
              }
              reader1.Close();
  
              watcher.EnableRaisingEvents = true;
  
          }
          else
          {
              StreamWriter sw = new StreamWriter(serviceStat, true);
              sw.WriteLine("File not found. Please start the Log Backup UI first.");
              sw.Close();
          }
      }
  </code></pre>
  
  <p>I don't know what keeps the windows service not starting, the windows form simulator worked fine. What seems to be the problem?</p>
  
  <p>UPDATE:
  After many trials I've noticed that using only a folder directory (w/out file), the windows service doesn't work. When I replaced the fileWatch variable with a specific file (including its directory), the windows service started. When I changed it back to a folder location, it didn't work. What I think is that folder locations doesn't work in a filewatcher.</p>
  
  <p>When I tried creating a new windows service that watches a folder location, it worked.. However, when I tried the same location in my original windows service, it didn't work! I was mindf$#*ed! It seems that I have to create a new windows service and build the installer everytime I place a new code/function.. This way I can keep track where I get an error.</p>