我在集成兩段代碼時遇到了一些麻煩。第一個檢查文件的大小,下一個循環通過SQL數據庫並查找文件的匹配名稱。我基本上想檢查它是否是一個新文件,或者自從我上次記錄它的一些數據後該文件已經改變。匹配文件的名稱和大小
此獲取每個文件的大小在目錄
// Make a reference to a directory.
DirectoryInfo di = new DirectoryInfo("C:\\Users");
// Get a reference to each file in that directory.
FileInfo[] fiArr = di.GetFiles();
// Display the names and sizes of the files.
MessageBox.Show("The directory {0} contains the following files:", di.Name);
foreach (FileInfo f in fiArr)
MessageBox.Show("The size of" + f.Name + " is " + f.Length + " bytes.");
這段代碼循環,直到它找到一個馬赫或直到所有條目一直看着低谷。
try
{
// LINQ query for all files containing the word '.txt'.
var files = from file in
Directory.EnumerateFiles("C:\\Users")
where file.ToLower().Contains(".txt")
select file;
foreach (var file in files)
{
//Get path to HH file
filename = System.IO.Path.GetFileName(file);
tempString = "";
//Keep looking trough database utill database empty or HH found
while (inc != numberOfSessions && (filename != tempString))
{
sessionRow = sessions.Tables["Sessions"].Rows[inc];
tempString = sessionRow.ItemArray.GetValue(1).ToString();
inc++;
}
可以說ItemAttay.GetValue(2)返回保存的文件大小。我怎樣才能最有效地保持while循環會如果
INC!= numberOfSessions & &(文件名!= tempString)& &(sessionRow.ItemArray.GetValue(2)== f.length)
感謝看看!
你關心一個文件的內容是否改變,但大小保持不變?如果是這樣,則「檢查文件大小是否已更改」標準將不起作用。也許計算一個哈希呢? – 2012-08-01 06:22:25
我只想知道它是一個新文件還是附加了一些東西。它永遠不會(我永遠不會猜到)變小。你將如何去計算散列? – 2012-08-01 06:28:27
內置.NET哈希函數的基類是[HashAlgorithm](http://msdn.microsoft.com/zh-cn/library/system.security.cryptography.hashalgorithm.aspx)。在http://support.microsoft.com/kb/307020上有一些非常古老的文檔描述計算文件哈希。 – 2012-08-01 06:34:04