title: Windows Service Exception When File.Copy()
body: <p>My service code resided in <code>OnStart()</code> throws Exception(I) and service is stopped. I don't know why is there any ex. thrown?.. This is my code:</p>
  
  <pre class="lang-C# prettyprint-override"><code>public Service1()
  {
      InitializeComponent();
  }
  
  Thread thread;
  
  protected override void OnStart(string[] args)
  {
      thread = new Thread(delegate()
      {
          string path = @"D:\levani\FolderListenerTest\ListenedFolder";
          FileSystemWatcher listener;
          listener = new FileSystemWatcher(path);
          listener.Created += new FileSystemEventHandler(listener_Created);
          listener.EnableRaisingEvents = true;
      });
      thread.Start();
  }
  
  public void listener_Created(object sender, FileSystemEventArgs e)
  {
      File.Copy(e.FullPath, @"D:\levani\FolderListenerTest\CopiedFilesFolder\F" + e.Name);
  }
  
  protected override void OnStop()
  {
      thread.Abort();
  }
  </code></pre>
  
  <p><strong>Log</strong> </p>
  
  <pre class="lang-js prettyprint-override"><code>Log Name:      Application
  Source:        .NET Runtime
  Date:          6/11/2012 5:33:27 PM
  Event ID:      1026
  Task Category: None
  Level:         Error
  Keywords:      Classic
  User:          N/A
  Computer:      Levan-PC
  Description:
  Application: FolderListenerService.exe
  Framework Version: v4.0.30319
  Description: The process was terminated due to an unhandled exception.
  Exception Info: System.IO.IOException
  Stack:
     at System.IO.__Error.WinIOError(Int32, System.String)
     at System.IO.File.InternalCopy(System.String, System.String, Boolean)
     at System.IO.File.Copy(System.String, System.String)
     at FolderListenerService.Service1.listener_Created(System.Object, System.IO.FileSystemEventArgs)
     at System.IO.FileSystemWatcher.OnCreated(System.IO.FileSystemEventArgs)
     at System.IO.FileSystemWatcher.NotifyFileSystemEventArgs(Int32, System.String)
     at System.IO.FileSystemWatcher.CompletionStatusChanged(UInt32, UInt32, System.Threading.NativeOverlapped*)
     at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32, UInt32, System.Threading.NativeOverlapped*)
  
  Event Xml:
  &lt;Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event"&gt;
    &lt;System&gt;
      &lt;Provider Name=".NET Runtime" /&gt;
      &lt;EventID Qualifiers="0"&gt;1026&lt;/EventID&gt;
      &lt;Level&gt;2&lt;/Level&gt;
      &lt;Task&gt;0&lt;/Task&gt;
      &lt;Keywords&gt;0x80000000000000&lt;/Keywords&gt;
      &lt;TimeCreated SystemTime="2012-06-11T14:33:27.000000000Z" /&gt;
      &lt;EventRecordID&gt;18314&lt;/EventRecordID&gt;
      &lt;Channel&gt;Application&lt;/Channel&gt;
      &lt;Computer&gt;Levan-PC&lt;/Computer&gt;
      &lt;Security /&gt;
    &lt;/System&gt;
    &lt;EventData&gt;
      &lt;Data&gt;Application: FolderListenerService.exe
  Framework Version: v4.0.30319
  Description: The process was terminated due to an unhandled exception.
  Exception Info: System.IO.IOException
  Stack:
     at System.IO.__Error.WinIOError(Int32, System.String)
     at System.IO.File.InternalCopy(System.String, System.String, Boolean)
     at System.IO.File.Copy(System.String, System.String)
     at FolderListenerService.Service1.listener_Created(System.Object, System.IO.FileSystemEventArgs)
     at System.IO.FileSystemWatcher.OnCreated(System.IO.FileSystemEventArgs)
     at System.IO.FileSystemWatcher.NotifyFileSystemEventArgs(Int32, System.String)
     at System.IO.FileSystemWatcher.CompletionStatusChanged(UInt32, UInt32, System.Threading.NativeOverlapped*)
     at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32, UInt32, System.Threading.NativeOverlapped*)
  &lt;/Data&gt;
    &lt;/EventData&gt;
  &lt;/Event&gt;
  </code></pre>