title: Unnecessary assignment of variables
body: <p>I have question about unnecessary assignments of variables.</p>
  
  <p>I have lot of functions and some of them when they catch exception they will write it into logfile.</p>
  
  <p>For example this is one of them(Just rename PC using powershell):</p>
  
  <pre><code>public static bool SetMachineName(string name)
  {
          try
          {
              Runspace rs;
              rs = RunspaceFactory.CreateRunspace();
              rs.Open();
  
              using (PowerShell ps = PowerShell.Create())
              {
                  ps.AddCommand("Rename-computer");
                  ps.AddParameter("newname", name);
                  ps.Runspace = rs;
                  ps.Invoke();
                  return true;
              }
          }
          catch (Exception ex)
          {
              LogWriter loger = new LogWriter(ex.ToString());
              return false;
          }
  }
  </code></pre>
  
  <p>And when im creating logwriter to write into file it says "Unnecessary assigments of a value to 'loger' but it does only in some function.</p>
  
  <p>For example im not getting this error in this function:</p>
  
  <pre><code>public static bool VPNAdapterExists(string VPNAdapterName)
      {
          try
          {
              // VPN adapters are stored in the rasphone.pdk
              // "C:\Users\Me\AppData\Roaming\Microsoft\Network\Connections\Pbk\rasphone.pbk"
              string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
                            @"\Microsoft\Network\Connections\Pbk\rasphone.pbk";
  
              const string pattern = @"\[(.*?)\]";
              var matches = Regex.Matches(File.ReadAllText(path), pattern)
                  .OfType&lt;Match&gt;()
                  .Select(m =&gt; m.Groups[0].Value)
                  .ToArray();
  
              bool has = matches.Contains(VPNAdapterName);
  
              if (has == true) { return true; }
              else { return false; }
  
          }
          catch(Exception ex)
          {
              LogWriter loger = new LogWriter(ex.ToString());
              return false;
          }
  
      }
  </code></pre>
  
  <p>Its a class with just bunch of functions sometimes i get this warning sometimes i dont. And only in "Catch" block im not getting this in "Try" blocks.</p>
  
  <p>I dont know if you need LogWriter class too so here it is:</p>
  
  <pre><code>public class LogWriter
  {
      private string m_exePath = string.Empty;
      public LogWriter(string logMessage)
      {
          LogWrite(logMessage);
      }
      public void LogWrite(string logMessage)
      {
          if (File.Exists("settings.xml"))
          {
              XmlSerializer xs = new XmlSerializer(typeof(Information));
              using (FileStream read = new FileStream("settings.xml", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
              {
                  Information info = (Information)xs.Deserialize(read);
  
                  bool LogginEnabled;
  
                  LogginEnabled = info.EnableLogging;
                  if (LogginEnabled == true)
                  {
                      m_exePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                      try
                      {
                          using (StreamWriter w = File.AppendText(m_exePath + "\\" + "log.txt"))
                          {
                              Log(logMessage, w);
                          }
                      }
                      catch (Exception ex)
                      {
                          MessageBox.Show(ex.ToString());
                      }
                  }
  
              }
          }
      }
  
      public void Log(string logMessage, TextWriter txtWriter)
      {
          try
          {
              txtWriter.Write("\r\n");
              txtWriter.Write("{0} {1}", DateTime.Now.ToLongTimeString(),
                  DateTime.Now.ToLongDateString());
              txtWriter.Write("  : {0}", logMessage);
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.ToString());
          }
      }
  }
  
  </code></pre>
  
  <p>Can someone explain what cause this problem and why is it so random?</p>
  
  <p>Thanks for all answers.</p>