Matt Dillane

Matt Dillane

  • NA
  • 15
  • 2k

Programatically populate a multi dimensional array

Apr 14 2015 1:37 PM
Hi All
 
I have only just started using C#, but I am having a problem I cannot find an answer to. I am sure it is obvious and simple.
I have created a class that loops through a set of folders from a root folder, then gets details of each file inside the next relevant folder. I have two nested for loops. When I get to the second loop, I want to fill the array with 6 elements (columns).
 
I have tried Tutorials, Lessons etc and cannot work it out.
 
Any advice would help. 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Windows.Forms;
using System.Diagnostics;
namespace WindowsFormsApplication2
{
class createData
{
private string rootPath2;
public createData()
{
rootPath2 = "";
}
public void createFolderData(string rootPath2)
{
//The dataList array is to hold the following
//{0} = Index
//{1} = Folder root
//{2} = Folder name
//{3} = File root
//{4} = File name
//{5} = File extension
string[,,,,,] dataList = new string[0,0,0,0,0,0];
int arrayIndexCounter = 0;
string[] directoryArray = Directory.GetDirectories(rootPath2);
//Start loop to first folder in the folder array
for (int j = 0; j <= directoryArray.GetUpperBound(0); j++)
{
Debug.Print("createData.cs. Main folders from root: "+directoryArray[j]);
//Create an array filled with the filenames from the folder array
string[] fileNames = Directory.GetFiles(directoryArray[j], "*.*");
//Start to loop through the file names contained in the folder
for (int k = 0; k <= fileNames.GetUpperBound(0); k++)
{
Debug.Print(">>> createData.cs. Files in folders: " + fileNames[k]);
arrayIndexCounter++;
string arrayIndexCounterString = arrayIndexCounter.ToString();
Debug.Print("$$$$$ Current number: " + (arrayIndexCounter++));
//Gets the main root of the start folder
string folderRootPath = directoryArray[j];
//Gets the root path of file in the folder
string fileRootPath = fileNames[k];
//Gets the root folder of the file
string folderName = Path.GetFileName(Path.GetDirectoryName(fileRootPath));
//Gets the file name and full path
string fileName = Path.GetFileNameWithoutExtension(fileNames[k]);
//Gets the file extension without the name
string fileExtension = Path.GetExtension(fileNames[k]);
dataList[0,1,2,3,4,5]={{arrayIndexCounterString},{folderRootPath},{folderName},{fileRootPath},{fileName},{fileExtension}};
}
}
Debug.Print("$$$$$ Final number: " + (arrayIndexCounter++));
}
}
}
 

Answers (3)