/// This is my code and my file already open in window i want to close it first and then delete pls help
var directory = new DirectoryInfo(path);
{
if (!IsFileLocked(file1, file) & file1.Name == file + ".xlsx")
File.Delete(path + file + ".xlsx"); // DELETE THE FILE BEFORE CREATING A NEW ONE.
}
public static Boolean IsFileLocked(FileInfo file, string fileName)
{
FileStream stream = null;
try
{
if (file.Name == fileName + ".xlsx")
{
using (stream = File.Open(file.FullName, FileMode.Open, FileAccess.Write, FileShare.Read))
{
}
}
}
catch (Exception ex)
{
return true;
}
finally
{
if (stream != null)
stream.Close();
}
return false;
}