添加声音文件进行构建,并找到路径?C# [英] Add sound files to build, and find path? C#
问题描述
在基于控制台的程序中,我想在其末尾使用声音文件,我能够使它正常工作,但是当我发布项目并尝试将其发送给其他人时,我无法将其包含在内声音文件,我该怎么做?
In my console based program I am wanting to use a sound file at the end of it, I was able to get that to work however when I publish the project and try send it to others I can't get it to include the sound file, how would I accomplish this?
推荐答案
- 将WAV文件添加到您的项目中(在解决方案资源管理器中右键单击您的项目,选择添加|现有项...)
- 在解决方案资源管理器中,转到您添加的文件的属性,并为构建操作"选择嵌入式资源".
- 将以下代码添加到您的控制台应用程序:
代码:
System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream s = a.GetManifestResourceStream("<assemblyname>.<wavfilename>.wav");
SoundPlayer player = new SoundPlayer(s);
player.PlaySync();
用程序集的名称替换< assemblyname>
,并用添加的WAV文件的名称替换< wavfilename>
.
replacing <assemblyname>
with the name of your assembly, and <wavfilename>
with the name of the WAV file that you added.
对于我的测试代码,该行如下所示:
For my test code, that line looked like this:
System.IO.Stream s = a.GetManifestResourceStream("ConsoleApp3.tada.wav");
因为我的程序集名称为"ConsoleApp3",而wav文件名为"tada.wav".
because my assembly name is "ConsoleApp3" and the wav file was called "tada.wav".
请注意,您还需要使用System.Media; 将添加到源代码文件.
Note you will also need to add a using System.Media;
to the source code file.
这篇关于添加声音文件进行构建,并找到路径?C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!