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");
}
如果我不把\ r在console.Write並使用Console.WriteLine不\在康壽窗口裏看到這個圖片[X]文件多次! 與第二行相同的是文本文件中的sw.WriteLine,我在那裏多次看到它。 如果文件不存在,我只想看一次。 爲什麼它顯示它如此ma手times腳?以及如何解決它?
謝謝。
爲什麼地球上你正在測試循環內存在的文件? –