My application shows images on screen (images based upon files on the local computer) and users can delete them if needed.
Every time I try to delete a file it results in the following error message:
"The process cannot access the file 'C:\\Users\\Dave\\Desktop\\Duplicate\\Swim.JPG' because it is being used by another process."
I understand the error message.
I have a UserControl
which accepts a file path (via a parameter in the constructor) and then binds it to it's (UserControl) DataContext
.
As part of debugging this issue I have found the issue is due to setting the DataContext within the UserControl. If I remove this.DataContext = this;
from within my UserControl then I can delete the file.
So, my TestUnit looks like
Ui.UserControls.ImageControl ic = new ImageControl(
@"C:\Users\Dave\Desktop\Duplicate\Swim.JPG");
try
{
File.Delete(@"C:\Users\Dave\Desktop\Duplicate\Swim.JPG");
}
catch (Exception ex)
{
Assert.Fail(ex.Message);
}
The UserControl CodeBehind
public ImageControl(string path)
{
this.FilePath = path;
this.DataContext = this; // removing this line allows me to delete the file!
InitializeComponent();
}
#region Properties
private string _filePath;
public string FilePath
{
get { return _filePath; }
set
{
_filePath = value;
OnPropertyChanged("FilePath");
}
}
If it matters, my UserControl XAML is using the 'Image' control, bound to 'FilePath'
I have tried making the UserControl null before deleting, this did not help.
I have tried adding the IDisposible Interface to my UserControl and within the Dispose()
method setting this.DataContext = null;
but this did not help.
What am I doing wrong? How can I delete this file (or more accurately, make it unused).