添加声音文件进行构建,并找到路径?C#

编程入门 行业动态 更新时间:2024-10-19 17:38:56
本文介绍了添加声音文件进行构建,并找到路径?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#

    本文发布于:2023-11-07 06:08:19,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1565740.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:路径   声音文件

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!