【问题标题】:How to use a string instead of the "name" for a properties.resources."name"如何为 properties.resources."name" 使用字符串而不是“name”
【发布时间】:2018-12-10 22:54:44
【问题描述】:

我有一堆存储在资源中的音频文件,因为我希望它们由程序携带,我想在创建时将每个文件分配给存储在 soundplayer 数组中的每个 soundplayer,每个资源名称与字符串相同存储在不同的数组中

strings[] names = new string[80];
Soundplayer[] sounds = new Soundplayer[80];
Soundplayer mySound;

for (int i = 0; i < sounds.Length; i++)
{
    sounds[i] = mySound = new Soundplayer(Properties.Resources.names[i])
}

它只是说资源不包含名称的定义,解决这个问题的方法是什么。只是资源._____不是字符串?

【问题讨论】:

  • 如果是我,我会在项目中添加一个 XML 文件作为“嵌入式资源”(右键单击->文件上的属性,然后将“构建操作”设置为“嵌入式”资源”)。然后,当我想稍后获取信息时,我会从应用程序的资源中提取文件并解析它。但是,这正是我的想法。
  • 它经过高度优化,可让您访问特定的命名资源。后门是使用Properties.Resources.ResourceManager.GetObject(names[i])。但这仍然需要你初始化你的 names[] 数组,呸。无论如何,在资源中填充这么多大文件并不是一个好主意,Directory.GetFiles() 变得更具吸引力。

标签: c# winforms soundplayer


【解决方案1】:

您可以使用Properties.Resources.ResourceManager.GetString访问它:

var name = names[i]; // make sure this is initialized
var filename = Properties.Resources.ResourceManager.GetString(name);
sounds[i] = mySound = new Soundplayer(filename);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-18
    • 2013-12-09
    • 1970-01-01
    • 1970-01-01
    • 2012-10-19
    • 1970-01-01
    • 2021-08-31
    相关资源
    最近更新 更多