2011-08-14 90 views
-1
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腳?以及如何解決它?

謝謝。

+2

爲什麼地球上你正在測試循環內存在的文件? –

回答

1

這是因爲您正在測試Y循環內的X文件。把該測試在外環改爲:

for (x = 0; x < images.Length - 1; x++) { 
    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 { 
    for (y = x + 1; y < images.Length; y++) { 
     ... 
0

您內環爲Y不是那麼目前你正在做的是打印在指數X的元素,內環內的y倍的X上的指數 - 即你爲了輸出語句的目的,在X,Y次處理元素而不是一次。

你應該做的是在第二個循環之前移動代碼塊。檢查X中的文件是否只存在一次就足夠了,直到X更改。所以請嘗試:

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 { 
    for (y = x + 1; y < images.Length; y++) 
    { 
     // Etc 
    } 
}