-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPanelComandDeleteFile.cs
More file actions
270 lines (255 loc) · 10.3 KB
/
Copy pathPanelComandDeleteFile.cs
File metadata and controls
270 lines (255 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
using netCommander.FileSystemEx;
using System.Runtime.InteropServices;
using System.ComponentModel;
namespace netCommander
{
class PanelComandDeleteFile : PanelCommandBase
{
public PanelComandDeleteFile()
{
ToolStripMenuItem menu = new ToolStripMenuItem();
menu.ShortcutKeys = Keys.F8;
menu.ShowShortcutKeys = true;
menu.Text = "Delete";
CommandMenu = menu;
}
private WIN32_FIND_DATA fs_data = new WIN32_FIND_DATA();
private bool force_readonly = false;
private bool supress_exceptions = false;
protected override void internal_command_proc()
{
QueryPanelInfoEventArgs e = new QueryPanelInfoEventArgs();
OnQueryCurrentPanel(e);
if (e.FocusedIndex == -1)
{
return;
}
DirectoryList dl=(DirectoryList)e.ItemCollection;
int[] sel_indices = e.SelectedIndices;
//we must cache selection
//becouse indexes will be change while deleting
List<string> sel_names = new List<string>();
if (sel_indices.Length > 0)
{
for (int i = 0; i < sel_indices.Length; i++)
{
sel_names.Add(Path.Combine(dl.DirectoryPath, dl.GetItemDisplayNameLong(sel_indices[i])));
}
}
else
{
sel_names.Add(Path.Combine(dl.DirectoryPath, dl.GetItemDisplayNameLong(e.FocusedIndex)));
}
//prepare dialog
DeleteFileDialog dialog = new DeleteFileDialog();
dialog.Text = "Delete files";
if (sel_names.Count == 1)
{
if (IOhelper.IsDirectory(sel_names[0]))
{
dialog.labelQuestion.Text = string.Format("Do you REALLY want to delete '{0}'? This directory and all its contents it will be destroyed for ever.", sel_names[0]);
}
else
{
dialog.labelQuestion.Text = string.Format("Do you REALLY want to delete '{0}'? The file will be destroyed for ever.", sel_names[0]);
}
}
else
{
dialog.labelQuestion.Text = string.Format("Do you REALLY want to delete {0} entries? All selected files and directories with the contents will be destroyed for ever.", sel_names.Count);
}
dialog.checkBoxForceReadonly.Checked = Options.DeleteReadonly;
dialog.checkBoxSupressExceptions.Checked = Options.DeleteSupressExceptions;
if (dialog.ShowDialog() != DialogResult.OK)
{
return;
}
//save user selection
Options.DeleteReadonly = dialog.checkBoxForceReadonly.Checked;
Options.DeleteSupressExceptions = dialog.checkBoxSupressExceptions.Checked;
force_readonly = dialog.checkBoxForceReadonly.Checked;
supress_exceptions = dialog.checkBoxSupressExceptions.Checked;
//and enum entries in sel_names and recursively delete each
foreach (string one_name in sel_names)
{
delete_file_recurs(one_name);
}
}
private void delete_file_recurs(string current_file)
{
bool readonly_changed = false;
int res=0;
int winErr = 0;
try
{
Wrapper.GetFileInfo(current_file, ref fs_data);
}
catch (Exception ex)
{
process_exceptions(ex);
}
if ((fs_data.dwFileAttributes & FileAttributes.Directory) == FileAttributes.Directory)
{
try
{
//this is deirectory
//current_file is directory
//enum entries and call recurs for each
string search_path = current_file;
search_path = search_path.TrimEnd(new char[] { Path.DirectorySeparatorChar });
search_path = search_path + Path.DirectorySeparatorChar + "*";
Wrapper.WIN32_FIND_DATA_enumerable fs_enum = new Wrapper.WIN32_FIND_DATA_enumerable(search_path);
foreach (WIN32_FIND_DATA data in fs_enum)
{
//skip refs to current and up dirs
if (data.cFileName == ".")
{
continue;
}
if (data.cFileName == "..")
{
continue;
}
delete_file_recurs(Path.Combine(current_file, data.cFileName));
}
//now directory current_file must be empty
//check attr
try
{
if ((fs_data.dwFileAttributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
{
if (force_readonly)
{
clear_readonly(current_file);
readonly_changed = true;
}
else
{
if (!supress_exceptions)
{
Messages.ShowMessage(string.Format("Cannot delete '{0}'. Directory have readonly attribute.", current_file));
return;
}
}
}
}
catch (Exception ex)
{
process_exceptions(ex);
}
//and delete
try
{
///////////////////////////////////////////////////////////////////////////////////////
/* DEBUG */
//if (current_file.EndsWith("netcommander", StringComparison.CurrentCultureIgnoreCase))
//{
// int j = 0;
// j++;
//}
///////////////////////////////////////////////////////////////////////////////////////
res = Native.RemoveDirectory(current_file);
if (res == 0)
{
//restore attr
if (readonly_changed)
{
set_readonly(current_file);
}
if (!supress_exceptions)
{
//throw exception
winErr = Marshal.GetLastWin32Error();
throw new Win32Exception(winErr);
}
}
}
catch (Exception ex)
{
process_exceptions(ex);
}
}
catch (Exception ex)
{
process_exceptions(ex);
}
}
else
{
//current_file is FILE
//check attributes
try
{
if ((fs_data.dwFileAttributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
{
if (force_readonly)
{
clear_readonly(current_file);
readonly_changed = true;
}
else
{
if (!supress_exceptions)
{
Messages.ShowMessage(string.Format("Cannot delete '{0}'. File have readonly attribute.", current_file));
return;
}
}
}
}
catch (Exception ex)
{
process_exceptions(ex);
}
//now try delete file
try
{
res = Native.DeleteFile(current_file);
if (res == 0)
{
//restore attr
if (readonly_changed)
{
set_readonly(current_file);
}
if (!supress_exceptions)
{
winErr = Marshal.GetLastWin32Error();
throw new Win32Exception(winErr);
}
}
}
catch (Exception ex)
{
process_exceptions(ex);
}
}
}
private void clear_readonly(string file_name)
{
FileSystemInfo fsi = IOhelper.GetFileSystemInfo(file_name);
FileAttributes fa_existing = fsi.Attributes;
FileAttributes fs_feeded=~((~fa_existing) | FileAttributes.ReadOnly);
fsi.Attributes = fs_feeded;
}
private void set_readonly(string file_name)
{
FileSystemInfo fsi = IOhelper.GetFileSystemInfo(file_name);
FileAttributes fa_existing = fsi.Attributes;
FileAttributes fa_needed = FileAttributes.ReadOnly | fa_existing;
fsi.Attributes = fa_needed;
}
private void process_exceptions(Exception ex)
{
if (!supress_exceptions)
{
Messages.ShowException(ex);
}
}
}
}