我有一個FileSystemWatcher監視從掃描設備接收TIF文件的目錄。檢查TIFF文件掃描是否完成
有必要檢查掃描過程是否完成,然後處理完全掃描的文件,否則,我的程序將處理不完整的文件。
我有類似:
private void OnFileCreated(...)
{
while(IsFileLocked(path))
Thread.Sleep(time);
// OK to read
}
這是正在發生的事情:
- Scanner creates the file
- FileSystemWatcher detects the file, but its in use
- Scanner reads the first page to the file
- Scanner releases the file
- My code leaves the while(IsFileLocked(path))
- My code reads the incomplete file (problem)
- Scanner adds more pages to the file
比方說,掃描儀掃描100頁,那麼,當 「OK讀」,該文件將不完整(剩99頁)。
所以,它有必要知道文件是否完整。也許等待一段時間以查看文件是否被修改,但是這段時間可能長達數小時,因爲掃描儀可以空閒掃描相同的TIF。其他解決方案將檢查TIF文件中的一些標誌,指示該文件不完整(我已經查找過但沒有發現任何內容)。
編輯:在問這裏之前,我已經讀了TIFF format documentation。我發現屬性PageNumber有趣,但不知道它是否會解決我的問題
什麼的'FileInUse'功能嗎? – vlad
您可以查詢掃描儀瞭解掃描完成時間嗎? – UnhandledExcepSean
它驗證如果任何程序正在讀取文件,我把這種方式來保持它簡單,但它做類似於:http://stackoverflow.com/questions/876473/is-there-a-way-to-check如果一個文件正在使用 這個方法的一個更好的名字是IsFileLocked –