The following snippet shows a simple way to delete specified file
private static bool DeleteFile(string sFileName)
{
try
{
if (File.Exists(sFileName) == true)
{
File.Delete(sFileName);
return true;
}
else
{
Console.WriteLine("File doesnot exist");
return false; }
}
catch (UnauthorizedAccessException ex)
{
// File in Use
Console.WriteLine("You do not have required permission for this operation " + ex.Message);
return false;
}
catch (IOException exIO)
{
// File in Use
Console.WriteLine("File in use " + exIO.Message);
return false;
}
catch (Exception ex)
{//Common Exception
Console.WriteLine(ex.Message);
return false;
}
}
Exception in File Delete (C#)
.NET Kill Method, C# File Class, C# Delete Method
See Also:
Move and Overwrite Files in C# (.NET)
Clean Temp (.tmp) and Word Backup files (.wbk) using Word VBA
Copying Files using .Net Functions
Good and Easy Code.
ReplyDeleteif (File.Exists(sFileName) == true)
ReplyDelete{
OR
if (File.Exists(sFileName))
{
Easy :-)
thank you
ReplyDelete