2013-10-17 64 views
0

有一个访问目录并通过SoundPlayer类访问文件的应用程序。 WAV。 我有以下C#:如何解决此System.UnauthorizedAccessException? C#

public void PlaySound() 
{ 
    try 
    { 
     while (true) 
     { 
      List<string> distinctMusicFile = GetMusicFile.Distinct().ToList(); 

      if (DataTableWorkCall.GetDataTableNew.Rows.Count > 0) 
      { 
       for (int i = 0; i < 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()); 
    } 
} 

此代码生成此异常:

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() 

日志生成:

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(); 
     } 

    } 

函数log.LogTxt(String)创建一个txt文件,这可以吗? 有关权限的问题? 任何人都可以帮助我吗?谢谢!

+0

'有关权限的问题?'我会说这是一个安全的赌注。确保该进程具有读取文件的权限。 – asawyer

+0

异常可能是从您的try/catch范围之外的其他线程抛出的。 – kcnygaard

+0

叹息......如果您查看堆栈跟踪,但是您没有在您的帖子中包含该方法,那么错误显然会被抛出到LogTxt中。请发布代码。 – tnw

回答

0

线

at AndonGestamp_FormMonitoramento.Utils.Log.LogTxt(System.String) 

表明例外从LogTxt茎。

您的结论是这是一个许可问题是正确的。
如果您无法通过目测检查代码找到问题,追踪到尽可能接近你失败的行,并开始ProcessMonitor找到你的代码试图到达的地方/内容。