What I'd like to do is get 6 pieces of file information from every file in a folder, and all folders beneath it. These pieces are:
- Type (you can do this in Access, it will return "Microsoft Excel Worksheet" or "Winzip File" or something, just like you see in the Explorer window, but I didn't see a property for this in C#)
- FullName
- Length
- CreationTime
- LastAccessTime
- LastWriteTime
I have the following code, which will spit out a list of files complete with path, but I'm trying to figure out how to get that additional info in my output. If it can be separated by a semicolon or something, that would be good as the eventual desire is to write this info to a table. Any help is appreciated.
private void GetMyFiles () {
string rootDirectory = @"" + txtPathToScan.Text + "";
string extension = "*";
List<string> files = GetFilesRecursively (rootDirectory, extension);
Console.WriteLine ("Got {0} files of extension {1} in directory {2}", files.Count, extension, rootDirectory);
foreach (string file in files) {
Console.WriteLine (file);
}
}
/// <summary>
/// This function calls itself recursively for each of the subdirectories that it finds
/// in the root directory passed to it. It returns files of the extension as specified
/// by the caller.
/// </summary>
/// <param name="rootDirectory">The directory from which the file list is sought.</param>
/// <param name="extension">The particular extension for which the file list is sought.</param>
/// <returns>A list of all the files with extension as specified by the caller. This list
/// includes the files in the current directory as well its sub-directories.</returns>
static List<string> GetFilesRecursively (string rootDirectory, string extension) {
// Uncomment this line only if you want to trace the control as it goes from
// sub-directory to sub-directory. Be ready for long scrolls of text output:
//Console.WriteLine ("Currently in directory {0}", rootDirectory);
// Create an output list:
List<string> opList = new List<string> ();
// Get all files in the current directory:
string[] allFiles = Directory.GetFiles (rootDirectory, "*." + extension);
// Add these files to the output list:
opList.AddRange (allFiles);
// Get all sub-directories in current directory:
string[] subDirectories = Directory.GetDirectories (rootDirectory);
// And iterate through them:
foreach (string subDir in subDirectories) {
// Get all the files from the sub-directory:
List<string> subDirFileList = GetFilesRecursively (subDir, extension);
// And add it to this list:
opList.AddRange (subDirFileList);
}
// Finally return the output list:
return opList;
}