开发者

.NET SoundPlayer QUestion

开发者 https://www.devze.com 2023-01-30 23:03 出处:网络
I was ho开发者_JAVA百科ping the code below would play the Fireball.wav file located in the same directory as the .exe.

I was ho开发者_JAVA百科ping the code below would play the Fireball.wav file located in the same directory as the .exe.

 SoundPlayer simpleSound = new SoundPlayer(@"Fireball.wav");
 simpleSound.Play();

I believe I'm setting up the wrong filepath.


You can do something like this:

var path = Path.Combine(
              Path.GetDirectoryName(Application.ExecutablePath) 
             ,"Fireball.wav");
SoundPlayer simpleSound = new SoundPlayer(path);
simpleSound.Play();


Using relative paths without a specified base path(i.e. relative to the current directory) is usually a bad idea.
The one common exception I can thing of is relative paths passed as a command line parameter into an application.

In particular the current directory is not identical to the application directory. It can be any directory on startup, and common dialogs(depending on their flags) can change it. Additionally it is a per process and not per thread variable, so another thread might change it at any time.

So I highly recommend creating an absolute path from your relative path before accessing it(See Dog Ears's post for how to do that)

0

精彩评论

暂无评论...
验证码 换一张
取 消