As u r opening the file before deleting the same file so as u open the file for writting then obviously it is used by the process ,so just delete that line
I m using following code for deleting the file and it is working very fine
string fileName = textBox1.Text.Trim();
try
{
if (File.Exists(fileName))
{
//File.OpenWrite(fileName);
File.Delete(fileName);
MessageBox.Show("File Deleted Successfully");
}
else
{
MessageBox.Show("File does not exists");
}
}
catch (FileNotFoundException ex)
{
MessageBox.Show(ex.Message.ToString());
}