This program will use C# and LINQ to iterate files, to query, group and order da
ID: 3807678 • Letter: T
Question
This program will use C# and LINQ to iterate files, to query, group and order data, and to create an XML document based on that data
Implementation You must follow these implementation guidelines:
1. Create a C# console application. This application has two command line arguments: A path to a folder and a name for a HTML report output file. The application collects all files with the same extension (converted to lower case) and determines for each extension, i.e. file type, the number of files and the total size of all files of this type
Explanation / Answer
Input Parameters:
1) Local Directory Path
2) Output File Name.txt
Program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace SampleConsoleApp1
{
class Program
{
static void Main(string[] args)
{
var localPath = args[0];
var fileName = args[1];
var Files = new DirectoryInfo(@localPath).GetFiles().ToList().OrderBy(f => f.Extension);
TextWriter tw = new StreamWriter(@localPath + fileName);
List<FileDetails> fileInfo = new List<FileDetails>();
List<string> fileTypes = new List<string>();
// to get all the distinct file Types
foreach (var file in Files)
{
if(!fileTypes.Contains(file.Extension.ToLower()))
{
fileTypes.Add(file.Extension.ToLower());
}
}
foreach (var type in fileTypes)
{
var fileCount = 0;
var totalSize = 0.0;
foreach (var file in Files)
{
if (type.Equals(file.Extension.ToLower()))
{
fileCount++;
totalSize += file.Length;
}
}
fileInfo.Add(new FileDetails { FileType = type, NumberOfFiles = fileCount, TotalSize = totalSize });
}
foreach (var info in fileInfo)
tw.WriteLine(info.FileType + ' ' + info.NumberOfFiles + ' ' + info.TotalSize);
tw.Close();
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
public class FileDetails
{
public string FileType { get; set; }
public int NumberOfFiles { get; set; }
public double TotalSize { get; set; }
}
Output: (For sample input what i have used in my sytem). there are two types of file(.c and .exe)
.c 2 1891
.exe 2 268212
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.