如果文件足夠小,應用程序可以處理文件的拷貝,而不是文件本身。另外,如果您的應用程序需要知道擁有的進程是否刪除了原始文件,請在該文件上設置一個FileSystemWatcher
(FSW)。當文件消失時,FSW可以設置一個標誌來中斷處理:
private bool _fileExists = true;
public void Process(string pathToOriginalFile, string pathToCopy)
{
File.Copy(pathToOriginalFile, pathToCopy);
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = pathToOriginalFile;
watcher.Deleted += new FileSystemEventHandler(OnFileDeleted);
bool doneProcessing = false;
watcher.EnableRaisingEvents = true;
while(_fileExists && !doneProcessing)
{
// process the copy here
}
...
}
private void OnFileDeleted(object source, FileSystemEventArgs e)
{
_fileExists = false;
}
應該提及您正在使用DELETE_ON_CLOSE/FILE_SHARE_DELETE。我懷疑這是非常常見的用法。 – 2010-09-07 22:56:56