Compare commits

...

2 Commits

Author SHA1 Message Date
jason.zhu 73bf46712d Finished Writing a Report with LINQ 2021-02-23 16:37:53 +11:00
jason.zhu d13bc6b34d Finished Writing a Report without LINQ 2021-02-23 16:32:29 +11:00
3 changed files with 85 additions and 0 deletions

View File

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30907.101
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Introduction", "Introduction\Introduction.csproj", "{53450129-90CC-4C40-BDB3-295F60669012}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{53450129-90CC-4C40-BDB3-295F60669012}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{53450129-90CC-4C40-BDB3-295F60669012}.Debug|Any CPU.Build.0 = Debug|Any CPU
{53450129-90CC-4C40-BDB3-295F60669012}.Release|Any CPU.ActiveCfg = Release|Any CPU
{53450129-90CC-4C40-BDB3-295F60669012}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3A6B85BA-05FF-44DA-B35D-12FDDCF35AC7}
EndGlobalSection
EndGlobal

View File

@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
</Project>

View File

@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Introduction
{
class Program
{
static void Main(string[] args)
{
string path = Directory.GetCurrentDirectory();
ShowLargeFilesWithoutLinq(path);
Console.WriteLine("***");
ShowLargeFilesWithLinq(path);
}
private static void ShowLargeFilesWithLinq(string path)
{
var query = new DirectoryInfo(path).GetFiles()
.OrderBy(f => f.Length)
.Take(5);
foreach (var file in query.Take(5))
{
Console.WriteLine($"{file.Name,-50} : {file.Length,10:N0}");
}
}
private static void ShowLargeFilesWithoutLinq(string path)
{
DirectoryInfo directory = new DirectoryInfo(path);
FileInfo[] files = directory.GetFiles();
Array.Sort(files, new FileInfoComparer());
for (int i = 0; i < 5; i++)
{
FileInfo file = files[i];
Console.WriteLine($"{file.Name, -20} : {file.Length, 10:N0}");
}
}
}
public class FileInfoComparer : IComparer<FileInfo>
{
public int Compare(FileInfo x, FileInfo y)
{
return y.Length.CompareTo(x.Length);
}
}
}