2011-08-14 16 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"); 
         } 

私はconsole.Write上の\ rを入れていけないとコンソル・ウィンドウで、この画像を見里\なしConsole.WriteLineをを使用している場合は、[X]を何回も提出! テキストファイルの2行目のsw.WriteLineと同じことが何度も見られます。 ファイルが存在しない場合は一度だけ見たいです。 なぜそれはそれほど時間がかかるのですか?そしてそれをどのように修正するのですか?

ありがとうございました。

+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だから、内部ループ内のY回あなたは、現在のインデックスXの要素を印刷しているやっているX.されていないあなたの内側のループのインデックス - あなた、すなわち出力文の目的のために、X、Yで1回ではなく要素を処理しています。

あなたがすべきことは、2番目のループの前にブロックを移動することです。 Xが変更されるまで、Xのファイルが1回だけ存在することを確認するだけで十分です。したがって、試してみてください:

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 
    } 
} 
関連する問題