How can I delete a file in C# e.g. C:\\test.txt, although apply the same kind of method like in batch files e.g.
if exist \"C:\\test.txt\"
dele
This will be the simplest way,
if (System.IO.File.Exists(filePath))
{
System.IO.File.Delete(filePath);
System.Threading.Thread.Sleep(20);
}
Thread.sleep will help to work perfectly, otherwise, it will affect the next step if we doing copy or write the file.
Another way I did is,
if (System.IO.File.Exists(filePath))
{
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
System.IO.File.Delete(filePath);
}