System.Media.SoundPlayer 在 .Net 中抛出 FileNotFoundException

问题描述

尝试在我的应用程序中播放 .wav 文件进行预览。

-> 正在执行以下代码

System.Media.soundplayer player = new System.Media.soundplayer();
player.soundLocation = this.UnderlyingAudioFile.FullName;
player.Play();

出现错误
System.IO.FileNotFoundException: '请确保指定位置存在声音文件。'

this.UnderlyingAudioFile.FullName;成为 System.IO.FileInfo 对象;

路径绝对正确。我检查了 3 次,可以从这里复制粘贴。UnderlyingAudioFile.FullName;

解决方法

当另一个线程无法访问文件时,实际上会发生此错误。

就我而言,我必须使用 fileshare.read 属性打开文件:

this.Reader = new BinaryReader(File.Open(path,FileMode.Open,FileAccess.Read,FileShare.Read));