/// <summary> /// Load a CSV file into an array of Dictionary objects. /// The first line of the CSV is taken to be headers, these values will be used as the indexes/keys to the dictionaries. /// Each subsequent line will be placed in a dictionary with the headers as keys /// </summary> /// <param name="strFileName">The path and file name to read</param> /// <returns>The array of dictionaries created when reading the CSV data</returns> public static Dictionary <String, String>[] LoadCSVFile(string strFileName) { string[] astrTextFile, astrLineValues; List <String> lstrHeaders, lstrValues; Dictionary <String, String>[] adctRetFile; Dictionary <String, String> dctOneLine; int iCnt; astrTextFile = LoadTextFile(strFileName); adctRetFile = new Dictionary <string, string> [astrTextFile.Length - 1]; //First line is the header, don't count it //Determine the headers lstrHeaders = (List <String>)RegEx.SplitOnMatches(astrTextFile[0], "([^,\"]*|[^,\"]*\"[^\"]*\"[^,\"]*)(,|$)"); //Loop through all lines building the dictionary, first line is headers skip it for (int iCtr = 1; iCtr < astrTextFile.Length; iCtr++) { dctOneLine = new Dictionary <string, string>(); lstrValues = (List <String>)RegEx.SplitOnMatches(astrTextFile[iCtr], "([^,\"]*|[^,\"]*\"[^\"]*\"[^,\"]*)(,|$)"); astrLineValues = lstrValues.ToArray(); iCnt = 0; foreach (string strCurrHeader in lstrHeaders) { //Trim white space and trailing commas string strCleanHeader = strCurrHeader.Trim(); if (strCleanHeader.EndsWith(",") == true) { strCleanHeader = strCleanHeader.Substring(0, strCleanHeader.Length - 1); strCleanHeader = strCleanHeader.Trim(); } astrLineValues[iCnt] = astrLineValues[iCnt].Trim(); if (astrLineValues[iCnt].EndsWith(",") == true) { astrLineValues[iCnt] = astrLineValues[iCnt].Substring(0, astrLineValues[iCnt].Length - 1); astrLineValues[iCnt] = astrLineValues[iCnt].Trim(); } dctOneLine.Add(strCleanHeader, astrLineValues[iCnt]); iCnt++; } //Add dictionary to array adctRetFile[iCtr - 1] = dctOneLine; } return(adctRetFile); }
/// <summary> /// Retrieves a list of all sub-directories in a directory that match the specified regular expression /// </summary> /// <param name="DirPath">Directory path to search</param> /// <param name="DirRegEx">Regular expression to apply to each directory name</param> /// <returns>The list of directory names that matched the regular expression</returns> static public IEnumerable <string> ListDirsInDir(string DirPath, string DirRegEx) { List <string> FilterDirList = new List <string>(); string[] FullFileList = Directory.GetDirectories(DirPath); string FileName; foreach (string FilePath in FullFileList) { FileName = Path.GetFileName(FilePath); if (RegEx.QuickTest(FileName, DirRegEx) == true) { FilterDirList.Add(FileName); } } return(FilterDirList); }