04 CSharp Advanced Streams Files and Directories Exercise
04 CSharp Advanced Streams Files and Directories Exercise
04 CSharp Advanced Streams Files and Directories Exercise
1. Even Lines
Write a program that reads a text file (e. g. text.txt) and prints on the console its even lines. Line numbers start
from 0. Use StreamReader. Before you print the result, replace {'-', ', ', '. ', '! ', '? '} with '@' and reverse the
order of the words.
Note: use the following structure:
namespace EvenLines
{
using System;
Console.WriteLine(ProcessLines(inputFilePath));
}
Examples
Input file: text.txt Output (at the console)
-I was quick to judge him, but it fault@ his wasn't it but him@ judge to
wasn't his fault. quick was @I
-Is this some kind of joke?! Is it? safer@ is It here@ hide @Quick@
-Quick, hide here. It is safer.
2. Line Numbers
Write a program that reads a text file (e. g. text.txt) and inserts line numbers in front of each of its lines and
count all the letters and punctuation marks. The result should be written to another text file (e. g. output.txt).
Use the static class File to read and write all the lines of the input and output files.
Note: use the following structure:
© SoftUni – about.softuni.bg. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Examples
text.txt output.txt
-I was quick to judge him, but it Line 1: -I was quick to judge him, but it
wasn't his fault. wasn't his fault. (37)(4)
-Is this some kind of joke?! Is Line 2: -Is this some kind of joke?! Is it?
it? (24)(4)
-Quick, hide here. It is safer. Line 3: -Quick, hide here. It is safer. (22)
(4)
namespace CopyBinaryFile
{
using System;
CopyFile(inputFilePath, outputFilePath);
}
4. Directory Traversal
Write a program that traverses a given directory for all files with the given extension. Search through the first level
of the directory only. Write information about each found file in a text file named report.txt and it should be
saved on the Desktop. The files should be grouped by their extension. Extensions should be ordered by the count
of their files descending, then by name alphabetically. Files under an extension should be ordered by their size.
report.txt should be saved on the Desktop. Ensure the desktop path is always valid, regardless of the user.
Note: use the following structure:
© SoftUni – about.softuni.bg. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
WriteReportToDesktop(reportContent, reportFileName);
}
Examples
Input Directory View report.txt
. .cs
--Mecanismo.cs - 0.994kb
--Program.cs - 1.108kb
--Nashmat.cs - 3.967kb
--Wedding.cs - 23.787kb
--Program - Copy.cs - 35.679kb
--Salimur.cs - 588.657kb
.txt
--backup.txt - 0.028kb
--log.txt - 6.72kb
.asm
--script.asm - 0.028kb
.config
--App.config - 0.187kb
.csproj
--01. Writing-To-Files.csproj - 2.57kb
.js
--controller.js - 1635.143kb
.php
--model.php - 0kb
© SoftUni – about.softuni.bg. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
namespace CopyDirectory
{
using System;
CopyAllFiles(inputPath, outputPath);
}
namespace ZipAndExtract
{
using System;
using System.IO;
© SoftUni – about.softuni.bg. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Hints
Use the ZipFile class.
The entry in the ZIP file should hold the file name only without its path.
© SoftUni – about.softuni.bg. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.