有没有办法发布使用System.Media Soundplayer在.NET Framework上运行的C#控制台应用?
当我发布我的应用程序并运行它时,它说它找不到通往 sound.wav
文件的路径,并且崩溃了。
我将声音文件放入 bin/debug/sound
文件夹中,然后在我的代码中使用它:
public void OpenInventory()
{
SoundPlayer inventory = new SoundPlayer("sound/selection2.wav");
inventory.Play();
try
{
var item = Item.itemList.SingleOrDefault(x => x.Amount == 0);
if (item != null)
{
Item.itemList.Remove(item);
}
var loot = Loot.lootList.SingleOrDefault(c => c.Amount == 0);
if (loot != null)
{
Loot.lootList.Remove(loot);
}
}
catch
{
inter.InventoryArray();
UseItem();
}
inter.InventoryArray();
UseItem();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将文件嵌入为资源,并使用资源流访问它。类似的内容:
要嵌入WAV文件,请将“构建操作”更改为解决方案资源管理器中的文件属性上的“嵌入式资源”。
使用此类嵌入式资源的棘手部分是找出正确的资源名称。默认情况下,它为资源使用类似命名空间的名称。因此,例如,如果您的应用程序的默认命名空间为“ mynamespace”,则该文件位于名为“ Sound”的文件夹中,并且该文件命名为“ Selection2.Wav”,则资源将被命名为“ myNamespace.sound.selection2” .wav“。
getManifestResourceStream
:https://learn.microsoft.com/en-us/dotnet/api/system.reflection.assembly.getmanifestresourcestream?view=netframework-4.8#system- Reflection-eSembly-getManifestResourceStream(System-string)soundplayer
流构造器: https://learn.microsoft.com/en-comrosoft.com/en-us/dotnet /PI/system.Media.soundplayer.-ctor?view=netFramework-4.8#system-media-soundplayer-ctor-ctor-ctor(System-io-stream)嵌入式资源名称: https://learn.microsoft.com/en-us/en-us/dotnet/core/core/resources/manifest-manifest-ector-文件名
You can embed the file as a resource and access it using a resource stream. Something like this:
To embed the WAV file, change "Build Action" to "Embedded Resource" on the file properties in solution explorer.
The tricky part of using such embedded resources is figuring out the correct resource name. By default it uses a namespace-like name for the resource. So for example, if the default namespace for your app is "MyNamespace", the file is in a folder named "Sound", and the file is named "selection2.wav", then the resource will be named "MyNamespace.Sound.selection2.wav".
GetManifestResourceStream
: https://learn.microsoft.com/en-us/dotnet/api/system.reflection.assembly.getmanifestresourcestream?view=netframework-4.8#system-reflection-assembly-getmanifestresourcestream(system-string)SoundPlayer
stream constructor: https://learn.microsoft.com/en-us/dotnet/api/system.media.soundplayer.-ctor?view=netframework-4.8#system-media-soundplayer-ctor(system-io-stream)Embedded resource names: https://learn.microsoft.com/en-us/dotnet/core/resources/manifest-file-names