-1

In my Project i have 525 Wave Files as Resource.

I want to Load all of this Wave Files into stream array and Store them For later use.

The problem is i Cant index Resources to Get those files.

Resources Sorted by name and i want to fill my array with this Order.

There is no Folder inside Resources. Just Wave files. Also there is no extra files in it. there is exactly 525 wave files.

I wish i could do something like this.

        Stream[] waves = new Stream[525];

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

Example Name of file names.

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

I also Tried this. But files names are not easy to be indexed.

Also if there is any way to put Resources in Dictionary it will become much easier.

        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];
        }
Community
  • 1
  • 1
M.kazem Akhgary
  • 18,645
  • 8
  • 57
  • 118
  • Instead of trying to sort them by name, put them in a dictionary and use the filename as a key. The sorting won't matter and you'll be able to get the one you want by its name, which is much simpler. – Pierre-Luc Pineault Jul 01 '15 at 18:19
  • can you give me solution or link to this solution? i cannot find anything about putting resources into dictionary. also im using winforms. thanks. it will become real easy if i be able to do so @Pierre-LucPineault – M.kazem Akhgary Jul 01 '15 at 18:36
  • where did you stored file names? – Ali Adlavaran Jul 01 '15 at 19:11
  • wave files are inside Resource folder(`Resource/xxx.wave`) with their name. i didnt store the file names inside code. actually i want to get the resources in loop because 525 files are too much. @AliAdl – M.kazem Akhgary Jul 01 '15 at 19:21
  • Did you tried storing them in a Dictionary ? – Ali Adlavaran Jul 01 '15 at 19:23
  • yes. my question is how to store them inside dictionary without writing 525 lines of code. i need to do this in loop. but i cant use index on Resources. @AliAdl – M.kazem Akhgary Jul 01 '15 at 19:25

1 Answers1

1

If you have access to the resource folder try this code:

        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!
        }
Ali Adlavaran
  • 3,697
  • 2
  • 23
  • 47