Sat 19 Jul 2008
File.Delete Command
Posted by admin under CSharp Commands |
Here is an example on how to delete files in the file sytem:-
public static void DeleteFile(string FilePath) { try { FileInfo TheFile = new FileInfo(HttpContext.Current.Server.MapPath(FilePath)); if (TheFile.Exists) { File.Delete(HttpContext.Current.Server.MapPath(FilePath)); } else { throw new FileNotFoundException(); } } catch (FileNotFoundException e) { LogEntry log = new LogEntry(); log.EventId = 701; log.Message = "Error Deleting File\r\n" + FilePath + "\r\n" + e.ToString() + "\r\n" + e.StackTrace; log.Categories.Add("General"); log.Severity = TraceEventType.Information; log.Priority = 5; Logger.Write(log); } catch (Exception e) { LogEntry log = new LogEntry(); log.EventId = 1301; log.Message = "Error Deleting File Exception\r\n" + FilePath + "\r\n" + e.ToString() + "\r\n" + e.StackTrace; log.Categories.Add("General"); log.Severity = TraceEventType.Information; log.Priority = 5; Logger.Write(log); } }
The example code above has some custom error exception handling that you may want to remove.
No Responses to “ File.Delete Command ”
Comments:
Leave a Reply
You must be logged in to post a comment.