title: How to fix this System.UnauthorizedAccessException? C#
body: <p>have an application that accesses a directory and via the SoundPlayer class I access a file. Wav.
  I have the following C #:</p>
  
  <pre><code>public void PlaySound()
  {
      try
      {
          while (true)
          {
              List&lt;string&gt; distinctMusicFile = GetMusicFile.Distinct().ToList();
  
              if (DataTableWorkCall.GetDataTableNew.Rows.Count &gt; 0)
              {
                  for (int i = 0; i &lt; distinctMusicFile.Count; i++)
                  {
                      StopSound();
                      player.SoundLocation = distinctMusicFile[i];
                      player.Play();
                      Thread.Sleep(Convert.ToInt32(ConfigurationSettings.AppSettings["MusicDuration"]) * 1000);
                      StopSound();
                  }
              }
              else
                  GetMusicFile.Clear();
          }
      }
      catch (ThreadAbortException e)
      {
          if (generateLog)
              log.LogTxt("Finished...\n");
      }
      catch (UnauthorizedAccessException e)
      {
          if (generateLog)
              log.LogTxt(e.ToString());
      }
  }
  </code></pre>
  
  <p>This code is generating this exception:</p>
  
  <pre><code>Application: AndonGestamp_FormMonitoramento.exe
  Framework Version: v4.0.30319
  Description: The process was terminated due to an unhandled exception.
  Exception Info: System.UnauthorizedAccessException
  Stack:
     at System.IO.__Error.WinIOError(Int32, System.String)
     at System.IO.FileStream.Init(System.String, System.IO.FileMode,         System.IO.FileAccess, Int32, Boolean, System.IO.FileShare, Int32, System.IO.FileOptions, SECURITY_ATTRIBUTES, System.String, Boolean, Boolean)
     at System.IO.FileStream..ctor(System.String, System.IO.FileMode, System.IO.FileAccess, System.IO.FileShare, Int32, System.IO.FileOptions)
     at System.IO.File.Create(System.String)
     at AndonGestamp_FormMonitoramento.Utils.Log.LogTxt(System.String)
     at AndonGestamp_FormMonitoramento.Utils.Music.PlaySound()
     at AndonGestamp_FormMonitoramento.Andon.ThreadPlayMusic()
     at System.Threading.ThreadHelper.ThreadStart_Context(System.Object)
     at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
     at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
     at System.Threading.ThreadHelper.ThreadStart()
  </code></pre>
  
  <p>Log generation:</p>
  
  <pre><code>public void LogTxt(string msg)
      {
          string logDirectory = ConfigurationSettings.AppSettings["Log"].ToString();
  
          if (!System.IO.File.Exists(logDirectory))
          {
              System.IO.File.Create(logDirectory).Close();
          }
  
          if (msg != null)
          {
              System.IO.TextWriter file = System.IO.File.AppendText(logDirectory);
              file.WriteLine(msg + " " + DateTime.Now + "\n");
              file.Close();
          }
  
      }
  </code></pre>
  
  <p>The function log.LogTxt (String) creates a txt file, can this be?
  Problems regarding permissions?
  Can anyone help me? Thanks!</p>