I'm stuck with a weird problem (which is probably my lack of knowledge), I present the offending 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. So if I run this with code where f is a video file playing in a mediaplayer it throws the exception. That works fine and as expected. Now when I close the mediaplayer it deletes the file!? Even though my application is long closed. Even when I close Visual Studio it still deletes the file, when I close the mediaplayer. As if some callback is being setup somewhere to make sure the file gets deleted at some point. This offcourse in unwanted behaviour. But I can't figure out what exactly goes wrong...
Result for now :
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 I still can do better between Delete and MoveTo, but I'll take my changes for now, shotgun coding.....
This is caused by the Offload Unused Apps feature, which helps you save storage space by removing apps that are not used frequently. You can switch if off in Settings - Apple ID & iCloud - iTunes & App Store.
Some apps are granted Android administrator access. These will prevent you from uninstalling them unless you revoke their administrator privilege. Sometimes, malware can also use administrator privilege to wreak havoc on your phone.
Even if a message appears to warn us that deleting the app will also delete the data, this usually only means that the data will be deleted from the device itself; it still exists on the developer's server.
You are getting the IOException
because the file cannot immediately be deleted or written to. However, when you call Delete()
, it seems the file is getting called for deletion.
Though the media player stops the file from being deleted while it is open, the file is still marked for deletion when it closes, regardless of whether your program is running. So when the media player closes the file is deleted.
You can check if the file is in use with the following code, taken from here. Make the Delete
and Copy
conditional on it not being locked, 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;
}
From the Windows SDK:
The DeleteFile function marks a file for deletion on close. Therefore, the file deletion does not occur until the last handle to the file is closed. Subsequent calls to CreateFile to open the file fail with ERROR_ACCESS_DENIED.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With