Home > Blockchain >  How would i be able to make a 2 dimensional array, where the indexes are of 2 different type of file
How would i be able to make a 2 dimensional array, where the indexes are of 2 different type of file

Time:11-18

I'm trying to get files from a directory into a 2 dimensional array, where .txt and .mp3 types are seperated like in the illustration at the bottom.

Here is the code:

    List<twoFiles> filesList = new();
    public void ListFiles(){
        string pathDir = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "folderPath");
        string[,] filePaths = Directory.GetFiles(pathDir, ["*.txt", "*.mp3"]);

        foreach (string[,] filePath in filePaths)
        {
            filesList.Add(filePath);
        }
    }
    public class twoFiles{
        
    }

How would i get the files from the directory in the array like illustrated in picture bellow?

Image of how the array can function

CodePudding user response:

Don't think of it as a "2 dimensional array" problem. You need a list of class objects with 2 filename properties.

Just a sketch:

// shorthand for a class with 2 readonly properties. 
public record TwoFiles(string Mp3File, string TxtFile);
string[] mp3Files = Directory.GetFiles(pathDir,"*.mp3");

List<TwoFiles> combinedFiles = mp3Files
   .Select(f => new TwoFiles(f, Path.ChangeExtension(f, ".txt")) )
   .ToList();

// optional, check
foreach(var combinedFile in combinedFiles)
{ 
   if (! File.Exists(combinedFile.TxtFile))
   {
      throw new ApplicationException($"Not found: {combinedFile.TxtFile}");
   }
}
  • Related