Deleting functions is deleted even when the application is closed

I am stuck in some strange problem (which I probably lack in knowledge), I present a violating code:

try { f.Delete(); fTemp.MoveTo(f.FullName); Console.WriteLine("INFO: Old file deleted new file moved in > {0}", f.FullName); } catch (IOException ex) { Console.WriteLine("ERROR: Output file has IO exception > {0}", f.FullName); Environment.ExitCode = 1; } 

f and fTemp are FileInfo objects. Therefore, if I run this with code where f is the video file playing in the media player, it throws an exception. This works fine and as expected. Now when I close the media player, it deletes the file !? Although my application has long been closed. Even when I close Visual Studio, it still deletes the file when I close the media player. It’s as if some kind of callback is being configured somewhere to make sure the file is deleted at some point. This rejection of unwanted behavior. But I can’t understand what exactly is going wrong ...

Result so far:

 if (!IsFileLocked(f)) { try { f.Delete(); fTemp.MoveTo(f.FullName); Console.WriteLine("INFO: Old file deleted new file moved in > {0}", f.FullName); } catch (IOException ex) { Console.WriteLine("ERROR: Output file has IO exception > {0}", f.FullName); Environment.ExitCode = 1; } catch (UnauthorizedAccessException ex) { Environment.ExitCode = 2; Console.WriteLine("ERROR: Output file is locked > {0}", f.FullName); } } else { Environment.ExitCode = 3; Console.WriteLine("ERROR: Couldn't delete file was locked"); } 

I know that I can still do better between Delete and MoveTo, but for now I will use my changes, fractional coding .....

+6
source share
3 answers

You get an IOException because the file cannot be deleted or written right away. However, when you call Delete() , it seems that the file is being called for deletion.

Although the media player stops deleting the file while it is opening, the file is still marked for deletion when it closes, regardless of whether your program is running. Therefore, when the media player closes, the file is deleted.

You can check if the file is used with the following code taken from here . Make the conditions Delete and Copy conditional if they are not blocked, and you should be good.

 try { if(!IsFileLocked(f)) { f.Delete(); fTemp.MoveTo(f.FullName); Console.WriteLine("INFO: Old file deleted new file moved in > {0}", f.FullName); } } protected virtual bool IsFileLocked(FileInfo file) { FileStream stream = null; try { stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None); } catch (IOException) { //the file is unavailable because it is: //still being written to //or being processed by another thread //or does not exist (has already been processed) return true; } finally { if (stream != null) stream.Close(); } //file is not locked return false; } 
+3
source

In the Windows SDK:

The DeleteFile function marks a file for deletion when closing. Therefore, file deletion does not occur until the last file descriptor is closed. Subsequent calls to CreateFile to open the file fail with the error ERROR_ACCESS_DENIED.

+1
source

As I understand it, you want your program to wait until the file is deleted, then delete it and move another file.

To do this, you can check to see if there is a handle in the file, but this requires unmanaged code. Another way is to use the answer method to this question: Is there a way to check if the file is being used?

 protected virtual bool IsFileLocked(FileInfo file) { FileStream stream = null; try { stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None); } catch (IOException) { //the file is unavailable because it is: //still being written to //or being processed by another thread //or does not exist (has already been processed) return true; } finally { if (stream != null) stream.Close(); } //file is not locked return false; } 

Before you start deleting a file, you can

A) until the file is deleted

 while(IsFileLocked(f)) { Thread.Sleep(100); } 

or B) cancel

 if (IsFileLocked(f)) { return; } 

If you chose A or B, it depends on your requirements.

0
source

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


All Articles