title: StreamWriter Sometimes Clears My File
body: <p>I'm making a program to add/remove entries to the Windows hosts file. So far it works fine except sometimes when I remove an item using my form, it will clear the entire hosts file, leaving nothing.</p>
  
  <p>Here is my code:</p>
  
  <pre><code> public void removeFromHosts(String key)
      {
          MessageBox.Show(key);
          if (key != "" || key != " ")
          {
              String[] strings = new String[1000];
  
              String all = File.ReadAllText(hostFile);
  
              strings = all.Split('\n');
  
  
              StreamWriter writer = new StreamWriter(hostFile);
  
              foreach (String s in strings)
              {
                  if (s != "255.255.255.255 " + key + " #Blocked by MyProgram")
                  {
                      writer.Write(s);
  
                  }
                  else
                  {
                      textBox1.Text += s;
                  }
              }
  
  
  
              writer.Close();
  
              blockurls.SelectedItems[0].Remove();
  
              MessageBox.Show("URL Successfully removed");
          }
          else
          {
              MessageBox.Show("Empty");
          }
      }
  </code></pre>
  
  <p>I'm basically checking each line in the textfile to see if it contains the url, if it doesn't contain the url then write that line to the file. What could be causing it to wipe the entire file?</p>