How to delete a file (folder) in C Sharp (C #)
Delete directly:
Using System.IO; .. String FilePath = @ "D:/.../xxx.xxx"; if (file.exists (FilePath)) {file.delete (filePath);} else {Console.WriteLine ("File not exist."); Console.ReadLine (); }
Delete to Recycle Bin:
Using System.Runtime.InteropServices; Namespace CSharp {Class Program {private Const int fo_delete = 3; Private Const int fof_allowundo = 0x40; Private Const int fof_noconfirmation = 0x0010; [StructLayout (layoutkind.sequential, CharSet = CharSet.Auto, Pack = 1)] public struct SHFILEOPSTRUCT {public IntPtr hwnd; [MarshalAs (UNMANAGEDTYPE.U4)] public int wfunc; public string pfrom; public string PTo; public short fflags; [MarshalAs (Unmanagedtype.bool)] public Bool fanyoperationsaborted; Public IntPtr hnamemappings; public string Lpszprogresstitle; [DllImport ("shell32.dll", CharSet = CharSet.Auto)] static extern int SHFileOperation (ref shfileopstruct FILEOP); static void Main (string[] args) {string filePath = @ ' d:/.../xxx.xxx '; if (file.exists (FilePath)) {shfileopstruct Fileop = new Shfileopstruct (); Fileop.wfunc = Fo_delete; Fileop.pfrom = FilePath + '/0 ' + '/0 '; Fileop.fflags = Fof_allowundo | Fof_noconfirmation; SHFileOperation (ref Fileop); Console.WriteLine ("Delete OK"); Console. ReadLine (); else {Console.WriteLine ("File not exist."); Console.ReadLine (); } } } }