Question #38757




As part of her business activities, Sharon has created a large number of files and folders.
Whenever she needs to view the details of the files stored in a folder, Sharon browses to the folder
using Windows Explorer. However, she finds it cumbersome to browse to each individual folder to
view the desired details. Therefore, she asks Hayley to modify the application to accept the name
of a folder and display the following details of the files located in the folder:
File name
File size
File creation date
Write the code that Hayley should write to create the desired file information viewer application.
[10 Marks]

Expert's answer

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Q38757
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(@"Enter the pass to the directory:(Example -> D:\)");
            string filePass = Console.ReadLine(); //folder pass
            if (System.IO.Directory.Exists(filePass)) // Does folder exist?
            {
                string[] fileNames = System.IO.Directory.GetFiles(filePass); //getting names of files
                Console.WriteLine("Directory: {0} contains {1} file(s)", filePass, fileNames.Length);
                for (int i = 0; i < fileNames.Length; i++)
                {
                    if (System.IO.File.Exists(fileNames[i]))
                    {
                        string name = "";
                        long size = 0;
                        DateTime cr_date;
                        string date = "";
                        try
                        {
                            name = fileNames[i].Substring(filePass.Length);
                            cr_date = System.IO.File.GetCreationTime(fileNames[i]); //creation date
                            size = (System.IO.File.ReadAllBytes(fileNames[i])).Length; //file size
                            date = cr_date.Day + "." + cr_date.Month + "." + cr_date.Year;
                            Console.WriteLine("{0,30} \t{1,10} byte(s) \t {2}", name, size, date);
                        }
                        catch
                        {
                            Console.WriteLine("{0,30} \t{1,10} byte(s) \t {2}", name, size, date);
                        }
                    }
                }
            }
            else Console.Write("Pass doesn't exist");
            Console.ReadKey();
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS