Created
September 25, 2024 06:48
-
-
Save fabienmw/875ac546ea501c3c1a74b7bc9dd0589d to your computer and use it in GitHub Desktop.
Process directories and sub directories
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace SPGIntegration.API.Workers; | |
public static class DirectoryProcessor | |
{ | |
// Given a parent directory write a function that will loop through the sub directories and convert each file in the directory to a byte[] array | |
public static void ConvertFilesToByteArrays(string parentDirectory) | |
{ | |
try | |
{ | |
// Get all subdirectories in the parent directory | |
string[] subDirectories = Directory.GetDirectories(parentDirectory); | |
// Iterate through each subdirectory | |
foreach (string subDirectory in subDirectories) | |
{ | |
DirectoryInfo directoryInfo = new DirectoryInfo(subDirectory); | |
// Get all files in the current subdirectory | |
string[] files = Directory.GetFiles(subDirectory); | |
// Iterate through each file | |
foreach (string file in files) | |
{ | |
// Convert file to byte array | |
byte[] fileBytes = FileToByteArray(file); | |
// You can do something with the byte[] array here | |
// For example, print the file name and the byte array length | |
Console.WriteLine($"Converted {file} to byte array. Length: {fileBytes.Length}"); | |
} | |
} | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine($"Error: {ex.Message}"); | |
} | |
} | |
// Function to convert a file to a byte array | |
public static byte[] FileToByteArray(string filePath) | |
{ | |
return File.ReadAllBytes(filePath); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment