mirror of
https://github.com/markjprice/cs11dotnet7.git
synced 2026-04-05 14:25:37 +00:00
Initial commit
This commit is contained in:
parent
17ad36d6ad
commit
3d241b2154
44 changed files with 1619 additions and 0 deletions
12
vscode/Chapter09/WorkingWithFileSystems/Program.Helpers.cs
Normal file
12
vscode/Chapter09/WorkingWithFileSystems/Program.Helpers.cs
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
partial class Program
|
||||
{
|
||||
static void SectionTitle(string title)
|
||||
{
|
||||
ConsoleColor previousColor = ForegroundColor;
|
||||
ForegroundColor = ConsoleColor.Yellow;
|
||||
WriteLine("*");
|
||||
WriteLine($"* {title}");
|
||||
WriteLine("*");
|
||||
ForegroundColor = previousColor;
|
||||
}
|
||||
}
|
||||
132
vscode/Chapter09/WorkingWithFileSystems/Program.cs
Normal file
132
vscode/Chapter09/WorkingWithFileSystems/Program.cs
Normal file
|
|
@ -0,0 +1,132 @@
|
|||
using static System.IO.Directory;
|
||||
using static System.IO.Path;
|
||||
using static System.Environment;
|
||||
|
||||
SectionTitle("Handling cross-platform environments and filesystems");
|
||||
|
||||
WriteLine("{0,-33} {1}", arg0: "Path.PathSeparator",
|
||||
arg1: PathSeparator);
|
||||
WriteLine("{0,-33} {1}", arg0: "Path.DirectorySeparatorChar",
|
||||
arg1: DirectorySeparatorChar);
|
||||
WriteLine("{0,-33} {1}", arg0: "Directory.GetCurrentDirectory()",
|
||||
arg1: GetCurrentDirectory());
|
||||
WriteLine("{0,-33} {1}", arg0: "Environment.CurrentDirectory",
|
||||
arg1: CurrentDirectory);
|
||||
WriteLine("{0,-33} {1}", arg0: "Environment.SystemDirectory",
|
||||
arg1: SystemDirectory);
|
||||
WriteLine("{0,-33} {1}", arg0: "Path.GetTempPath()",
|
||||
arg1: GetTempPath());
|
||||
WriteLine("GetFolderPath(SpecialFolder");
|
||||
WriteLine("{0,-33} {1}", arg0: " .System)",
|
||||
arg1: GetFolderPath(SpecialFolder.System));
|
||||
WriteLine("{0,-33} {1}", arg0: " .ApplicationData)",
|
||||
arg1: GetFolderPath(SpecialFolder.ApplicationData));
|
||||
WriteLine("{0,-33} {1}", arg0: " .MyDocuments)",
|
||||
arg1: GetFolderPath(SpecialFolder.MyDocuments));
|
||||
WriteLine("{0,-33} {1}", arg0: " .Personal)",
|
||||
arg1: GetFolderPath(SpecialFolder.Personal));
|
||||
|
||||
SectionTitle("Managing drives");
|
||||
|
||||
WriteLine("{0,-30} | {1,-10} | {2,-7} | {3,18} | {4,18}",
|
||||
"NAME", "TYPE", "FORMAT", "SIZE (BYTES)", "FREE SPACE");
|
||||
|
||||
foreach (DriveInfo drive in DriveInfo.GetDrives())
|
||||
{
|
||||
if (drive.IsReady)
|
||||
{
|
||||
WriteLine(
|
||||
"{0,-30} | {1,-10} | {2,-7} | {3,18:N0} | {4,18:N0}",
|
||||
drive.Name, drive.DriveType, drive.DriveFormat,
|
||||
drive.TotalSize, drive.AvailableFreeSpace);
|
||||
}
|
||||
else
|
||||
{
|
||||
WriteLine("{0,-30} | {1,-10}", drive.Name, drive.DriveType);
|
||||
}
|
||||
}
|
||||
|
||||
SectionTitle("Managing directories");
|
||||
|
||||
// define a directory path for a new folder
|
||||
// starting in the user's folder
|
||||
string newFolder = Combine(
|
||||
GetFolderPath(SpecialFolder.Personal), "NewFolder");
|
||||
|
||||
WriteLine($"Working with: {newFolder}");
|
||||
|
||||
// check if it exists
|
||||
WriteLine($"Does it exist? {Exists(newFolder)}");
|
||||
|
||||
// create directory
|
||||
WriteLine("Creating it...");
|
||||
CreateDirectory(newFolder);
|
||||
WriteLine($"Does it exist? {Exists(newFolder)}");
|
||||
Write("Confirm the directory exists, and then press ENTER: ");
|
||||
ReadLine();
|
||||
|
||||
// delete directory
|
||||
WriteLine("Deleting it...");
|
||||
Delete(newFolder, recursive: true);
|
||||
WriteLine($"Does it exist? {Exists(newFolder)}");
|
||||
|
||||
SectionTitle("Managing files");
|
||||
|
||||
// define a directory path to output files
|
||||
// starting in the user's folder
|
||||
string dir = Combine(
|
||||
GetFolderPath(SpecialFolder.Personal), "OutputFiles");
|
||||
|
||||
CreateDirectory(dir);
|
||||
|
||||
// define file paths
|
||||
string textFile = Combine(dir, "Dummy.txt");
|
||||
string backupFile = Combine(dir, "Dummy.bak");
|
||||
WriteLine($"Working with: {textFile}");
|
||||
|
||||
// check if a file exists
|
||||
WriteLine($"Does it exist? {File.Exists(textFile)}");
|
||||
|
||||
// create a new text file and write a line to it
|
||||
StreamWriter textWriter = File.CreateText(textFile);
|
||||
textWriter.WriteLine("Hello, C#!");
|
||||
textWriter.Close(); // close file and release resources
|
||||
WriteLine($"Does it exist? {File.Exists(textFile)}");
|
||||
|
||||
// copy the file, and overwrite if it already exists
|
||||
File.Copy(sourceFileName: textFile,
|
||||
destFileName: backupFile, overwrite: true);
|
||||
|
||||
WriteLine(
|
||||
$"Does {backupFile} exist? {File.Exists(backupFile)}");
|
||||
|
||||
Write("Confirm the files exist, and then press ENTER: ");
|
||||
ReadLine();
|
||||
|
||||
// delete file
|
||||
File.Delete(textFile);
|
||||
WriteLine($"Does it exist? {File.Exists(textFile)}");
|
||||
|
||||
// read from the text file backup
|
||||
WriteLine($"Reading contents of {backupFile}:");
|
||||
StreamReader textReader = File.OpenText(backupFile);
|
||||
WriteLine(textReader.ReadToEnd());
|
||||
textReader.Close();
|
||||
|
||||
SectionTitle("Managing paths");
|
||||
|
||||
WriteLine($"Folder Name: {GetDirectoryName(textFile)}");
|
||||
WriteLine($"File Name: {GetFileName(textFile)}");
|
||||
WriteLine("File Name without Extension: {0}",
|
||||
GetFileNameWithoutExtension(textFile));
|
||||
WriteLine($"File Extension: {GetExtension(textFile)}");
|
||||
WriteLine($"Random File Name: {GetRandomFileName()}");
|
||||
WriteLine($"Temporary File Name: {GetTempFileName()}");
|
||||
|
||||
SectionTitle("Getting file information ");
|
||||
|
||||
FileInfo info = new(backupFile);
|
||||
WriteLine($"{backupFile}:");
|
||||
WriteLine($"Contains {info.Length} bytes");
|
||||
WriteLine($"Last accessed {info.LastAccessTime}");
|
||||
WriteLine($"Has readonly set to {info.IsReadOnly}");
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="System.Console" Static="true" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Loading…
Add table
Add a link
Reference in a new issue