Home
Manage Your Code
Snippet: static class to delete files to basket (C#)
Title: static class to delete files to basket Language: C#
Description: static class to delete files to basket Views: 107
Author: - - Date Added: 4/28/2008
Copy Code  
1using System.Runtime.InteropServices;
2    
3    /// <summary>
4    /// pinvoke from windows to delete file to basket
5    /// </summary>
6    static class DeleteToBasket
7    {
8        private const int FO_DELETE = 3;
9        private const int FOF_ALLOWUNDO = 0x40;
10        private const int FOF_NOCONFIRMATION = 0x0010;
11
12        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 1)]
13        public struct SHFILEOPSTRUCT
14        {
15            public IntPtr hwnd;
16            [MarshalAs(UnmanagedType.U4)]
17            public int wFunc;
18            public string pFrom;
19            public string pTo;
20            public short fFlags;
21            [MarshalAs(UnmanagedType.Bool)]
22            public bool fAnyOperationsAborted;
23            public IntPtr hNameMappings;
24            public string lpszProgressTitle;
25        }
26
27        [DllImport("shell32.dll", CharSet = CharSet.Auto)]
28        static extern int SHFileOperation(ref SHFILEOPSTRUCT FileOp);
29
30        public static void MoveToBin(string path)
31        {
32            SHFILEOPSTRUCT fileop = new SHFILEOPSTRUCT();
33            fileop.wFunc = FO_DELETE;
34            fileop.pFrom = path + '\0' + '\0';
35            fileop.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION;
36            SHFileOperation(ref fileop);
37        }
38    }
Usage
DeleteTobasket.MoveToBin("c:\\test.txt");
Notes
Not my idea - found it in the net - not sure where