using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication_FileComparison
{
class Program
{
static void Main(string[] args)
{
//renameFiles();
duplicateFilesFinder();
}
public static string duplicateFilesFinder()
{
Console.WindowWidth = 107;
byte[] a, b;
StreamWriter sw;
string sf;
string[] images;
int x = 0;
int y = 0;
sw = new StreamWriter(@"d:\stream.txt");
sf = @"d:\test";
images = Directory.GetFiles(sf, "*.jpg");
for (x = 0; x < images.Length - 1; x++)
{
for (y = x + 1; y < images.Length; y++)
{
Console.Write("Working on file " + images[x] + " please wait\r");
if (!File.Exists(images[x]))
{
Console.Write("The file " + images[x] + " is not exist\r");
sw.WriteLine("The file " + images[x] + " is not exist");
}
else
{
if (File.Exists(images[y]))
{
a = File.ReadAllBytes(images[x]);
b = File.ReadAllBytes(images[y]);
if (a.Length == b.Length)
{
Console.WriteLine("File " + images[x] + " is the same size as" + " " + images[y]);
sw.WriteLine("File " + images[x] + " is the same size as" + " " + images[y]);
File.Delete(images[y]);
Console.WriteLine("File " + images[y] + " have been deleted");
sw.WriteLine("File " + images[y] + " have been deleted");
}
}
}
}
}
sw.Close();
Console.WriteLine(Environment.NewLine + "Process finished please press any key to continue");
Console.ReadKey();
return sf;
}
これはアールの問題である:なぜループ内のテキストファイルに書き込む行が何度もテキストファイルに書き込まれるのですか?
if (!File.Exists(images[x]))
{
Console.Write("The file " + images[x] + " is not exist\r");
sw.WriteLine("The file " + images[x] + " is not exist");
}
私はconsole.Write上の\ rを入れていけないとコンソル・ウィンドウで、この画像を見里\なしConsole.WriteLineをを使用している場合は、[X]を何回も提出! テキストファイルの2行目のsw.WriteLineと同じことが何度も見られます。 ファイルが存在しない場合は一度だけ見たいです。 なぜそれはそれほど時間がかかるのですか?そしてそれをどのように修正するのですか?
ありがとうございました。
なぜループ内に存在するファイルをテストしていますか? –