【问题标题】:How to fix this System.UnauthorizedAccessException? C#如何修复此 System.UnauthorizedAccessException? C#
【发布时间】:2013-10-26 09:40:04
【问题描述】:

有一个访问目录的应用程序并通过 SoundPlayer 类访问文件。哇。 我有以下 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文件,可以这样吗? 关于权限的问题? 谁能帮我?谢谢!

【问题讨论】:

  • Problems regarding permissions? 我会说这是一个安全的赌注。确保进程对文件具有读取权限。
  • 异常可能是从另一个线程抛出的,超出了您的 try/catch 范围。
  • 叹息...如果您查看堆栈跟踪但您没有在帖子中包含该方法,那么LogTxt显然会引发错误。请发布它的代码。
  • 显然,您的应用程序没有写入权限来创建您的 appSettings 中提到的日志文件。
  • 所以....您是否已确认您拥有logDirectory 的权限?是只读的吗?

标签: c# exception permissions


【解决方案1】:

线

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

表明异常源于 LogTxt。

您认为这是权限问题的结论是正确的。
如果您无法通过对代码的目视检查发现问题;跟踪到尽可能接近失败的行并启动ProcessMonitor 以查找您的代码试图到达的位置/内容。

【讨论】:

    【解决方案2】:

    可能应用程序在一个帐户下运行,而该帐户没有目录的写入权限。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-12
      • 1970-01-01
      • 2022-01-11
      • 2010-12-22
      • 2011-12-30
      • 2021-01-29
      • 1970-01-01
      相关资源
      最近更新 更多