Not sure if this is the cause of your problem right now, but your problems might be caused because you don't have the habit to Dispose objects that implement IDisposable.
If an object implements IDisposable, then the designer of the object thought that it holds scarce resources. It should be Disposed as soon as possible.
One of the problems I see, when people forget to Dispose the access to a file, is that you can't delete it, because it is still in use.
string bitmapFileName = "MyTestFile.bmp";
Bitmap testBitmap = new Bitmap(bitmapFileName);
... // do something with the bitmap. Or not, if you don't want to
// after a while you don't need the bitmap anymore
testBitmap = null;
System.IO.File.Delete(testBitMap); // Exception! File still in use
Try to make it a habit to Dispose objects that implement IDisposable:
string fileName = outputPathText.Text+@"\test.txt";
using (TextWriter textWriter = File.Create(fileName))
{
textWriter.WriteLine("The very first line!");
}
That's it: no need to Flush nor Close, the Dispose of the textWriter will do that for you.
Similarly:
using (var bmp = new Bitmap(testBitmap))
{
... // do something with it, or not if you don't want to
}
// now the bitmap can be deleted
File.Delete(testBitmap);
I'm not familiar with the CommonOpenFileDialog, and couldn't easily find the Microsoft description for it, but if if implements IDisposable, like every Form does, it should also be disposed:
using (var dialog = new CommonOpenFileDialog())
{
dialog.InitialDirectory = "C:\\Users";
dialog.IsFolderPicker = true;
if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
{
MessageBox.Show("You selected: " + dialog.FileName);
this.outputPathText.Text = dialog.FileName;
}
}
Dispose will clean up everything in the CommonOpenFileDialog and free up all resources used .