0

我正在编写一个 Windows 应用程序。我的表单中有媒体播放器。当我将 URL 设置为类似“fsgfbnhgb”(有问题!)时,播放器本身会显示错误。但这并不好。我有“try”和“catch”块,并且当 URL 为“fgdsfgasgfas”时,在 try 块中没有任何异常!因为玩家自己会捕捉到异常。如何自己为媒体播放器设置错误?

try
{
  player.URL = "fdgsdgsdg";
  player.Ctlcontrols.play();
}
catch
{
  snd.Play();
  MessageBox.Show("An error occurred during reading the alarm music path. Check that its directory is valid.", "Alarm music error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

谢谢你们

4

1 回答 1

0

您可以先在 try{} catch{} 中尝试检查文件是否存在

try{
    string url = "sdahgjk";
   if (!File.Exists(url))
        throw new Exception("Exception Message Here");
   player.Url = url;
   player.Ctlcontrols.play();
        }
        catch
        {
            snd.Play();
            MessageBox.Show("An error occurred during reading the alarm music path. Check that its directory is valid.", "Alarm music error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
于 2012-09-07T17:43:36.230 回答