2012-05-29 196 views

回答

3

我知道的最簡單的方法是在System.IO名稱空間中使用PathFile類。

你可以使用Path.Combine方法將你的目標目錄的路徑與文件的查找(由Path.GetFileName方法返回)名稱合併:

string dest_file = Path.Combine(dest_dir, Path.GetFileName(source_file)); 

在這一點上,你可以簡單地檢查是否dest_fileFile.Exists方法存在:

if (File.Exists(dest_file)) 
{ 
    // You can get file properties using the FileInfo class 
    FileInfo info_dest = new FileInfo(dest_file); 
    FileInfo info_source = new FileInfo(source_file); 

    // And to use the File.OpenRead method to create the FileStream 
    // that allows you to compare the two files 
    FileStream stream_dest = info_dest.OpenRead(); 
    FileStream stream_source = info_source.OpenRead(); 

    // Compare file streams here ... 
} 

Here解釋如何使用的FileStream比較兩個文件的文章。

也有檢查,如果該文件在目標目錄中,看看在Directory類,特別是對方法Directory.GetFiles一種替代方案:

foreach (string dest_file in Directory.GetFiles(dest_dir)) 
{ 
    // Compare dest_file name with source_file name 
    // and so on... 
} 
+0

謝謝,一個真正全面的迴應。 – Nick

1

myFile提取文件名,使用Path.Combine爲DestinationDir +您的文件名創建新的路徑,然後,檢查文件是否存在,使用File.Exists

用於比較兩個文件的嘗試:

public static IEnumerable<string> ReadLines(string path) 
public static IEnumerable<string> ReadLines(string path, Encoding encoding) 
bool same = File.ReadLines(path1).SequenceEqual(File.ReadLines(path2)); 

檢查該主題:How to compare 2 files fast using .NET?

相關問題