0

I have a requirement to access a folder path and read the file and folder properties using Shel32.Shell with C# code. I have written the login and I am able to read below the information and generate that info in the excel sheet. However, I am badly stuck at 2 places:

  1. I am not getting the information who (User name or User id) last accessed the file or folder.
  2. I am not able to iterate within the folder, the structure is nested there are files and folders within folders as it is a nested structure.

My Requirement: My program should be additionally provide username who last accessed file or folder and most importantly the program should be able to read the file and properties of all nested folders and files. Currently it only reads the info for folders and files which are on the root.

My code:

using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Security.AccessControl;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
using Shell32;

static void Main(string[] args)
{
    List<string> fileDetails = new List<string>();
    DataTable dt = new DataTable();
    DataRow dtrow;
    Console.Title = "Extended file properties.";
    List<string> arrHeaders = new List<string>();
    Shell32.Shell shell = new Shell32.Shell();
    Shell32.Folder objFolder;
    objFolder = shell.NameSpace(@"\\C:\Department");

    for (int i = 0; i < short.MaxValue; i++)
    {
        string header = objFolder.GetDetailsOf(null, i);
        if (String.IsNullOrEmpty(header))
            break;

        arrHeaders.Add(header);
    }

    dt.Columns.AddRange(new DataColumn[9] { 
        new DataColumn("Name", typeof(string)),
        new DataColumn("Item type", typeof(string)),
        new DataColumn("Authors", typeof(string)),
        new DataColumn("Owner", typeof(string)),
        new DataColumn("Date created", typeof(string)) ,
        new DataColumn("Date modified", typeof(string)),
        new DataColumn("Date accessed", typeof(string)) ,
        new DataColumn("File description", typeof(string)) ,
        new DataColumn("Creators", typeof(string)) 
    });

    foreach (Shell32.FolderItem item in objFolder.Items())
    {
        dtrow = dt.NewRow();
        for (int i = 0; i < arrHeaders.Count; i++)
        {
            if (arrHeaders[i].ToString().Equals("Name") || arrHeaders[i].ToString().Equals("Item type") || 
                arrHeaders[i].ToString().Equals("Authors") || arrHeaders[i].ToString().Equals("Owner") || 
                arrHeaders[i].ToString().Equals("Date created") || arrHeaders[i].ToString().Equals("Date modified") || 
                arrHeaders[i].ToString().Equals("Date accessed") ||  arrHeaders[i].ToString().Equals("File description") || arrHeaders[i].ToString().Equals("Creators"))
            {
                var K = objFolder.GetDetailsOf(item, i);
                switch (arrHeaders[i])
                {
                    case "Name":
                    case "Item type":
                    case "Authors":
                    case "Owner":
                    case "Date created":
                    case "Date modified":
                    case "Date accessed":
                    case "Creators":
                    case "File description":
                        //add headers
                        dtrow[arrHeaders[i]] = K;
                        break;

                    default:
                        // code block
                        break;
                }
                //Add headers
                var J = arrHeaders[i];
            }
        }
        //Add rows to table
        dt.Rows.Add(dtrow);
    }
}

Sample output in the excel file: enter image description here

Hoodlum
  • 950
  • 2
  • 13
iftekharus
  • 21
  • 2

0 Answers0