Вывести имя самого большого файла в каталоге - C#
Формулировка задачи:
нужно переделать программу так, чтобы вместо самого старого файла в папке My documents выводилось имя наибольшего файла
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
Console.WriteLine(path);
Console.ReadLine();
string[] files = Directory.GetFiles(path);
foreach (string str in files)
{
Console.WriteLine("{0}, {1}", str, str.Length);
}
Console.ReadLine();
Console.WriteLine("Введите имя создаваемого файла:");
string name_file = Console.ReadLine();
FileInfo fi = new FileInfo(path + name_file);
StreamWriter sw = fi.CreateText();
foreach (string str in files)
{
sw.WriteLine("Имя файла: {0}, его размер: {1}", str, str.Length);
}
sw.Close();
}
}
}Решение задачи: «Вывести имя самого большого файла в каталоге»
textual
Листинг программы
static void Main(string[] args)
{
string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
DirectoryInfo di = new DirectoryInfo(path);
var bigestFile = di.GetFiles().OrderBy(f => f.Length).Last();
Console.WriteLine("Самый большой файл в {0}: {1} ({2}Кб)", path, bigestFile.Name, bigestFile.Length/1024);
Console.ReadKey();
}