1

ディレクトリにアクセスするアプリケーションがあり、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 ファイルを作成しますが、これは可能ですか? パーミッションに関する問題? 誰でも私を助けることができますか?ありがとう!

4

2 に答える 2

0

この線

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

例外が LogTxt に起因することを示しています。

許可の問題であるというあなたの結論は正しいです。
コードを目視検査しても問題が見つからない場合。失敗した行のできるだけ近くまでトレースし、 ProcessMonitorを開始して、コードが到達しようとしている場所/対象を見つけます。

于 2013-10-17T18:49:51.737 に答える