Reputation: 2146
Its quite weird,i have a console application that reads media file and extract some data from media file using third party dll (MediaInfo.dll)
and then i upload that file to some other location from the source then finally i delete that file in the souce once everything is don.
Below is my code for deletion.
GC.Collect();
FileInfo[] fiListImages = (new DirectoryInfo(mfsData.Images)).GetFiles(String.Format("*{0}*", sp.Story.Slug));
foreach (FileInfo fi in fiListImages)
{
bool ImageFolder = fi.Directory.Name.Contains("Images");
if (ImageFolder)
{
File.Delete(fi.FullName);
}
}
FileInfo[] fiListMedia = (new DirectoryInfo(mfsData.EncodedMedia)).GetFiles(String.Format("*{0}*", sp.Story.Slug), SearchOption.AllDirectories);
foreach (FileInfo fi in fiListMedia)
{
if (sp.Profile.Name == "Comedy" && fi.FullName.Contains(@"\Comedy"))
{
File.Delete(fi.FullName);
}
else if (sp.Profile.Name == "Actuib" && ((fi.FullName.Contains(@"\Action") || (fi.FullName.Contains(@"\Syndicated")))))
{
File.Delete(fi.FullName);
}
}
I just want to call the garbage collector before start deletion ( i thought this will help me to remove all the locks but am wrong).
The code is working really good some times and sometimes it throws this exception "Process cannot access the file because it is being used by another process".
I can suspect another place when i load the media file to the third party library like below
videoInterrogator.LoadFile(filename);
logger.Info("video interrogar extract the video files");
message.AppendFormat(messageFormat, "FileSize", videoInterrogator.GetFileSize(), Environment.NewLine);
message.AppendFormat(messageFormat, "MaxBitRate", videoInterrogator.maxBitRate(), Environment.NewLine);
logger.Info("Video Extract done");
after get in to this piece of code when i try to delete that file manually i get the same error message and am not sure how to close the connection forcefully to release the file.
Any help much appreciated.
Upvotes: 2
Views: 744
Reputation: 17855
More information about videoInterrogator
would definitely be useful. Try checking its documentation. If it implements IDisposable
try calling Dispose()
on the object once you're done. If it's a COM object call Marshal.ReleaseComObject()
.
Upvotes: 2