简体   繁体   English

如何修复这个 System.UnauthorizedAccessException? C#

[英]How to fix this System.UnauthorizedAccessException? C#

have an application that accesses a directory and via the SoundPlayer class I access a file.有一个访问目录的应用程序,并通过 SoundPlayer 类访问一个文件。 Wav.哇。 I have the following C #:我有以下 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());
    }
}

This code is generating this exception:此代码正在生成此异常:

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

Log generation:日志生成:

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

    }

The function log.LogTxt (String) creates a txt file, can this be?函数log.LogTxt(String)创建一个txt文件,可以吗? Problems regarding permissions?权限问题? Can anyone help me?任何人都可以帮助我吗? Thanks!谢谢!

The line线

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

shows that the exception stems from LogTxt.表明异常源于 LogTxt。

Your conclusion that it is a permission problem is correct.您认为这是权限问题的结论是正确的。
If you cannot find the problem through visual inspection of your code;如果您无法通过目视检查您的代码发现问题; trace to as close as you can to the row that fails and start ProcessMonitor to find where/what your code tries to reach.跟踪到尽可能接近失败的行并启动ProcessMonitor以查找您的代码尝试到达的位置/内容。

也许在某个帐户下运行的应用程序对该目录没有写权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WinForms中的System.UnauthorizedAccessException C# - System.UnauthorizedAccessException C# in winforms 在LINQ中C#处理System.UnauthorizedAccessException - C# Handle System.UnauthorizedAccessException in LINQ 忽略文件夹C#(System.UnauthorizedAccessException) - Ignore folder C# (System.UnauthorizedAccessException) 解压bz2文件时如何修复System.UnauthorizedAccessException? - How to fix System.UnauthorizedAccessException when decompressing bz2 file? 文件中的 System.UnauthorizedAccessException。创建 c# - System.UnauthorizedAccessException in File.Create c# System.UnauthorizedAccessException:拒绝访问路径 (UWP C#) - System.UnauthorizedAccessException: Access to the path is denied (UWP C#) Windows应用商店C#:System.UnauthorizedAccessException写入图像文件 - Windows Store C#: System.UnauthorizedAccessException writing image file System.UnauthorizedAccessException C#Windows 10电话仿真器 - System.UnauthorizedAccessException C# Windows 10 Phone Emulator C#-UWP-System.UnauthorizedAccessException:“访问被拒绝”。 - C# - UWP - System.UnauthorizedAccessException: 'Access is denied.' 删除dll时出现System.UnauthorizedAccessException(C#) - System.UnauthorizedAccessException while deleting dll (C#)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM