Saturday, November 1, 2008

Delete Files using C# / Delete Files using .NET

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)

VBA Send File to Recycle Bin

Clean Temp (.tmp) and Word Backup files (.wbk) using Word VBA

Copying Files using .Net Functions

Delete Temporary Files

3 comments: