To delete a file in C#, we can use the File.Delete()
method from the System.IO
namespace. Here's an example:
main.cs613 chars20 lines
In this code, we first specify the path of the file that we want to delete. Then, we use the File.Delete()
method to delete the file.
It's important to use error handling while accessing and deleting files. In this example, we wrap the File.Delete()
method in a try-catch block to handle any exceptions that may occur during the deletion process. We catch the IOException
and UnauthorizedAccessException
to handle file access and permission issues respectively, and any other exception is caught by the last catch
block.
gistlibby LogSnag