将资源转换为数组或字典

Convert Resources into Array or Dictionary

在我的项目中,我有 525 Wave 文件作为资源。

我想将所有这些 Wave 文件加载到流数组中并存储它们以备后用。

问题是我无法索引资源以获取这些文件。

资源按名称排序,我想用这个顺序填充我的数组。

资源中没有文件夹。只是 Wave 文件。里面也没有多余的文件。正好有525个wave文件。

我希望我能做这样的事情。

        Stream[] waves = new Stream[525];

        for (int i = 0; i < waves.Length; i++)
        {
            waves[i] = Resources[i];
        }

文件名的示例名称。

A00,A01,A02,A03,.....B01,B02,B03,....C01....

我也试过了this。但是文件名不容易被索引

此外,如果有任何方法可以将资源放入词典中,它将变得更加容易。

        Stream[] waves = new Stream[525];
        Dictionary<int, Stream> Res = new Dictionary<int, Stream>();

        for (int i = 0; i < waves.Length; i++)
        {
            waves[i] = Res[i];
        }

如果您有权访问资源文件夹,请尝试此代码:

        string[] files = Directory.GetFiles(@"resources", "*.wav");
        Dictionary<string, Stream> dic = new Dictionary<string, Stream>();
        foreach (var file in files)
        {
            byte[] data = File.ReadAllBytes(file);
            MemoryStream stream = new MemoryStream(data);
            dic.Add(Path.GetFileNameWithoutExtension(file), stream);
        }

        foreach (var item in  dic.OrderBy(d=>d.Key))
        {
            // here store item which has been ordered by name!
        }