我只需要一個文件從遠程服務器複製到本地PC,如果:文件副本更新
- 本地PC上沒有目標文件呢。
- 服務器上的源文件比本地PC上的目標文件更新。
源文件是4 MB,所以我想避免每次都複製。
// File copy if target doesn't exist or source is newer:
if (File.Exists(filenameSource))
{
if (File.Exists(filenameTarget))
{
DateTime dateSource = File.GetLastWriteTimeUtc(filenameSource);
DateTime dateTarget = File.GetLastWriteTimeUtc(filenameTarget);
if (dateTarget < dateSource)
{
File.Copy(filenameSource, filenameTarget, true);
}
}
else
{
File.Copy(filenameSource, filenameTarget);
}
}
我的問題是:
難道上面的代碼還是要流4 MB的數據,以獲取源的更改的時間標記?
比較修改後的時間戳足以滿足我所要做的事情嗎?還是我應該比較創建的時間戳?
(#2似乎是一個愚蠢的問題,但如果我把一個新的源文件,修改的時間戳比目標的修改日期和時間舊?)
獎勵:
如果我在VBScript中編寫上述代碼會怎樣?下面的代碼是否需要傳輸4 MB的數據才能創建fileSource
對象?
if filesys.FileExists(strSource) then
if filesys.FileExists(strTarget) then
set fileSource = filesys.GetFile(strSource)
set fileTarget = filesys.GetFile(strTarget)
dateSource = fileSource.DateLastModified
dateTarget = fileTarget.DateLastModified
if dateTarget < dateSource then
filesys.CopyFile strSource, strTarget, true
end if
else
filesys.CopyFile strSource, strTarget, false
end if
end if
+1:好吧,我正在解釋你的答案,說我可以得到:創建,修改和大小**沒有流4 MB **。但是,我需要流4 MB才能計算散列。謝謝! – JohnB 2010-11-12 18:38:54
正確。這正是我所說的。 – Stu 2010-11-12 18:55:48