File deletion detection in open file

I open the file with read access and allowing subsequent read | write | remove access to the file sharing file (file tail). If the file is deleted during processing, is there a way to detect that the file is awaiting deletion (see Section " http://msdn.microsoft.com/en-us/library/aa363858(v=VS.85).aspx )? If any Either an external process (ownership process) issued a deletion, I want to close my descriptor as soon as possible in order to allow deletion of the file so as not to interfere with any logic in the ownership process.

I am in C # and do not see a method for detecting pending deletion. A file was opened using a FileStream object. Is there a way to detect deletion in C # or in some other Windows features?

+3
source share
4 answers

I would use a different signaling mechanism. (I make the assumption that access to the file is under your control, and not from a closed external program, mainly because of the flags used.)

The only "solution" within these boundaries that I can think of is a file access poll and an exception check (if any) that you return. Perhaps something is much more complex (at a lower level than the win32 API file?!?), But this is already following the path of "uhg" :-)

0
source

FileSystemWatcher, , , "" ; IS , FileSystemWatcher , , . ( ), , , FileStream , , .

- , FileInfo . FileInfos , , . NotifyFilter , . , OnDeleted. , , , - -, , FileStream. ; , , . , , .

0

, , . , , , 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;
}
0

, . , / , oplocks . , , ( , API- .., ).

0

Source: https://habr.com/ru/post/1763640/


All Articles