MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
DeleteFiles.cs
1using MyCaffe.basecode;
2using System;
3using System.Collections.Generic;
4using System.Diagnostics;
5using System.IO;
6using System.Linq;
7using System.Runtime.InteropServices;
8using System.Text;
9using System.Threading;
10using System.Threading.Tasks;
11
12namespace MyCaffe.db.image
13{
14#pragma warning disable 1591
15
16 public class DeleteFiles
17 {
18 Log m_log;
19 CancelEvent m_evtCancel;
20
21 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 1)]
22 struct SHFILEOPSTRUCT
23 {
24 public IntPtr hwnd;
25 [MarshalAs(UnmanagedType.U4)]
26 public int wFunc;
27 public string pFrom;
28 public string pTo;
29 public short fFlags;
30 [MarshalAs(UnmanagedType.Bool)]
31 public bool fAnyOperationsAborted;
32 public IntPtr hNameMappings;
33 public string lpszProgressTitle;
34 }
35
36 [DllImport("shell32.dll", CharSet = CharSet.Auto)]
37 static extern int SHFileOperation(ref SHFILEOPSTRUCT FileOp);
38
39 const int FO_DELETE = 3;
40 const int FOF_ALLOWUNDO = 0x40;
41 const int FOF_NOCONFIRMATION = 0x10;
42
43 public DeleteFiles(Log log, CancelEvent evtCancel)
44 {
45 m_log = log;
46 m_evtCancel = evtCancel;
47 }
48
49 public bool DeleteDirectory(string strDir)
50 {
51 if (!Directory.Exists(strDir))
52 return false;
53
54 Stopwatch sw = new Stopwatch();
55 string[] rgstrFiles = Directory.GetFiles(strDir);
56
57 sw.Start();
58
59 for (int i = 0; i < rgstrFiles.Length; i++)
60 {
61 File.Delete(rgstrFiles[i]);
62 //SHFILEOPSTRUCT shf = new SHFILEOPSTRUCT();
63 //shf.wFunc = FO_DELETE;
64 //shf.fFlags = FOF_ALLOWUNDO + FOF_NOCONFIRMATION;
65 //shf.pFrom = strDir + '\0' + '\0';
66 //SHFileOperation(ref shf);
67
68 if (m_evtCancel.WaitOne(0))
69 return false;
70
71 if (sw.Elapsed.TotalMilliseconds > 1000)
72 {
73 m_log.Progress = (double)i / (double)rgstrFiles.Length;
74 m_log.WriteLine("deleting " + i.ToString("N0") + " of " + rgstrFiles.Length.ToString("N0") + "...");
75 sw.Restart();
76 }
77 }
78
79 rgstrFiles = Directory.GetFiles(strDir);
80 if (rgstrFiles.Length == 0)
81 Directory.Delete(strDir);
82
83 return true;
84 }
85 }
86
87#pragma warning restore 1591
88}
The CancelEvent provides an extension to the manual cancel event that allows for overriding the manua...
Definition: CancelEvent.cs:17
bool WaitOne(int nMs=int.MaxValue)
Waits for the signal state to occur.
Definition: CancelEvent.cs:290
The Log class provides general output in text form.
Definition: Log.cs:13
void WriteLine(string str, bool bOverrideEnabled=false, bool bHeader=false, bool bError=false, bool bDisable=false)
Write a line of output.
Definition: Log.cs:80
double Progress
Get/set the progress associated with the Log.
Definition: Log.cs:147
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Definition: Annotation.cs:12
The MyCaffe.db.image namespace contains all image database related classes.
Definition: Database.cs:18
The MyCaffe namespace contains the main body of MyCaffe code that closesly tracks the C++ Caffe open-...
Definition: Annotation.cs:12