Visual Studio不会写入输出文件。 代码工作正常,结果显示在控制台上,而不显示在文件上。 试图更改文件目的地和目录,但没有结果。
这是我的代码:
string[] files = Directory.GetFiles("C:\\Users\\a.petkov\\Downloads\\FILES\\",
"*.*", SearchOption.TopDirectoryOnly);
foreach (string file in files)
{
Console.WriteLine(file);
}
Console.WriteLine();
Dictionary<string, Dictionary<string, decimal>> dict = new Dictionary<string, Dictionary<string, decimal>>();
foreach (string file in files)
{
//decimal length = (new System.IO.FileInfo(file).Length) / 1024;
decimal filesize = Decimal.Divide(file.Length, 1024);
string extension = Path.GetExtension(file);
string name = Path.GetFileName(file);
if (dict.ContainsKey(extension))
{
dict[extension].Add(name, filesize);
}
else
{
dict.Add(extension, new Dictionary<string, decimal>() { { name, filesize } });
}
}
string wayToFile = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\report.txt";
using (StreamWriter writer = new StreamWriter(wayToFile))
{
foreach (var item in dict.OrderByDescending(v => v.Value.Count).ThenBy(k => k.Key))
{
Console.WriteLine('.' + item.Key);
foreach (var kvp in item.Value)
{
Console.WriteLine($"--{kvp.Key} - {kvp.Value}kb");
}
}
}
答案 0 :(得分:2)
第36和39行
Console.WriteLine('.' + item.Key);
Console.WriteLine($"--{kvp.Key} - {kvp.Value}kb");
应该是
writer.WriteLine('.' + item.Key);
writer.WriteLine($"--{kvp.Key} - {kvp.Value}kb");