回答編集履歴

1

見直しキャンペーン中

2023/07/21 08:22

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -1,309 +1,149 @@
1
1
  最初は実行ファイルにドラッグ&ドロップの場合、画面なしかなと思いましたが出るんですね?
2
-
3
-
4
2
 
5
3
  戻り値で消したフォルダを返すようにするといいんじゃないでしょうか。
6
4
 
7
-
8
-
9
- ```C#
10
-
11
- // Form1.cs
5
+ ```cs:Form1.cs
12
-
13
-
14
-
15
6
  using System;
16
-
17
7
  using System.Collections.Generic;
18
-
19
8
  using System.IO;
20
-
21
9
  using System.Linq;
22
-
23
10
  using System.Windows.Forms;
24
11
 
25
-
26
-
27
12
  namespace WindowsFormsApp1
28
-
29
13
  {
30
-
31
14
  public partial class Form1 : Form
32
-
33
15
  {
34
-
35
16
  public Form1()
36
-
37
17
  {
38
-
39
18
  InitializeComponent();
40
-
41
19
  // デザイナで設定できる プロパティウィンドウ-動作
42
-
43
20
  //this.listBox1.AllowDrop = true;
44
-
45
-
46
21
 
47
22
  string[] files = Environment.GetCommandLineArgs();
48
23
 
49
-
50
-
51
24
  if(files.Length > 1)
52
-
53
25
  {
54
-
55
26
  Process processObj = new Process();
56
-
57
27
  // ↓string[]の仲間 消されたフォルダパス ↓いっこ飛ばす
58
-
59
28
  IEnumerable<string> deleteFolders = processObj.Other_Process(files.Skip(1));
60
-
61
29
  listBox1.Items.AddRange(deleteFolders.ToArray());
62
-
63
30
  }
64
-
65
31
  }
66
32
 
67
-
68
-
69
33
  private void listBox1_DragEnter(object sender, DragEventArgs e)
70
-
71
34
  {
72
-
73
35
  // ダメな場合を先に見てreturnすると{}が減って見やすい
74
-
75
36
  if(!e.Data.GetDataPresent(DataFormats.FileDrop))
76
-
77
37
  {
78
-
79
38
  e.Effect = DragDropEffects.None;
80
-
81
39
  return;
82
-
83
40
  }
84
-
85
-
86
41
 
87
42
  string[] paths = (string[])e.Data.GetData(DataFormats.FileDrop);
88
43
 
89
-
90
-
91
44
  foreach(string path in paths)
92
-
93
45
  {
94
-
95
46
  // 選択にフォルダ以外(ファイル)が入っていたら禁止
96
-
97
47
  if(!Directory.Exists(path))
98
-
99
48
  {
100
-
101
49
  e.Effect = DragDropEffects.None;
102
-
103
50
  return;
104
-
105
51
  }
106
-
107
52
  }
108
53
 
109
-
110
-
111
54
  e.Effect = DragDropEffects.All;
112
-
113
55
  }
114
56
 
115
-
116
-
117
57
  private void listBox1_DragDrop(object sender, DragEventArgs e)
118
-
119
58
  {
120
-
121
59
  string[] selectFolders = (string[])e.Data.GetData(DataFormats.FileDrop, false);
122
-
123
-
124
60
 
125
61
  Process processObj = new Process();
126
62
 
63
+ // ↓string[]の仲間 消されたフォルダパス
64
+ IEnumerable<string> deleteFolders = processObj.Other_Process(selectFolders);
65
+ listBox1.Items.AddRange(deleteFolders.ToArray());
66
+ }
67
+ }
68
+ }
69
+ ```
127
70
 
71
+ ```cs:Process.cs
72
+ using System.Collections.Generic;
73
+ using System.IO;
74
+ using System.Linq;
128
75
 
76
+ namespace WindowsFormsApp1
77
+ {
78
+ public class Process
79
+ {
80
+ // ↓消したフォルダを戻り値で返すように
81
+ public IEnumerable<string> Other_Process(IEnumerable<string> folders)
82
+ {
129
- // ↓string[]の仲間 消されたフォルダパス
83
+ // ↓string[]の仲間 こちらは後から追加できる
84
+ List<string> deleteFolders = new List<string>();
130
85
 
86
+ foreach(string folder in folders)
87
+ {
88
+ string[] subFolders;
89
+ try
90
+ {
91
+ // アクセスできないフォルダの場合例外
131
- IEnumerable<string> deleteFolders = processObj.Other_Process(selectFolders);
92
+ subFolders = Directory.GetDirectories(folder);
93
+ }
94
+ catch
95
+ {
96
+ // アクセスできないフォルダは無視
97
+ continue;
98
+ }
132
99
 
100
+ foreach(string subFolder in subFolders)
101
+ {
102
+ // とりあえず数字が入っているフォルダを削除するつもりになる
103
+ if(subFolder.Any(char.IsDigit))
104
+ {
105
+ //DeleteDirectory(subFolder);
133
- listBox1.Items.AddRange(deleteFolders.ToArray());
106
+ deleteFolders.Add(subFolder);
107
+ }
108
+ }
109
+ }
134
110
 
111
+ return deleteFolders;
135
112
  }
136
113
 
114
+ private void DeleteDirectory(string dir)
115
+ {
116
+ // 読み取り専用ファイルがあると例外
117
+ // http://dobon.net/vb/dotnet/file/deletedirectory.html
118
+ Directory.Delete(dir, true);
119
+ }
137
120
  }
138
-
139
121
  }
140
-
141
122
  ```
142
123
 
143
-
144
-
145
- ```C#
146
-
147
- // Process.cs
124
+ ```cs:Program.cs
148
-
149
-
150
-
151
- using System.Collections.Generic;
152
-
153
- using System.IO;
125
+ using System;
154
-
155
- using System.Linq;
126
+ using System.Windows.Forms;
156
-
157
-
158
127
 
159
128
  namespace WindowsFormsApp1
160
-
161
129
  {
162
-
163
- public class Process
130
+ static class Program
164
-
165
131
  {
166
-
132
+ [STAThread]
167
- // ↓消したフォルダを戻り値で返すように
133
+ static void Main()
168
-
169
- public IEnumerable<string> Other_Process(IEnumerable<string> folders)
170
-
171
134
  {
172
-
173
- // ↓string[]の仲間 こちらは後から追加できる
174
-
175
- List<string> deleteFolders = new List<string>();
176
-
177
-
178
-
179
- foreach(string folder in folders)
180
-
181
- {
182
-
183
- string[] subFolders;
184
-
185
- try
186
-
187
- {
188
-
189
- // アクセスできないフォルダの場合例外
190
-
191
- subFolders = Directory.GetDirectories(folder);
192
-
193
- }
194
-
195
- catch
196
-
197
- {
198
-
199
- // アクセスできないフォルダは無視
200
-
201
- continue;
202
-
203
- }
204
-
205
-
206
-
207
- foreach(string subFolder in subFolders)
135
+ Application.EnableVisualStyles();
208
-
209
- {
210
-
211
- // とりあえず数字が入っているフォルダを削除するつもりになる
212
-
213
- if(subFolder.Any(char.IsDigit))
214
-
215
- {
216
-
217
- //DeleteDirectory(subFolder);
136
+ Application.SetCompatibleTextRenderingDefault(false);
218
-
219
- deleteFolders.Add(subFolder);
137
+ Application.Run(new Form1());
220
-
221
- }
222
-
223
- }
224
-
225
- }
226
-
227
-
228
-
229
- return deleteFolders;
230
-
231
138
  }
232
-
233
-
234
-
235
- private void DeleteDirectory(string dir)
236
-
237
- {
238
-
239
- // 読み取り専用ファイルがあると例外
240
-
241
- // http://dobon.net/vb/dotnet/file/deletedirectory.html
242
-
243
- Directory.Delete(dir, true);
244
-
245
- }
246
-
247
139
  }
248
-
249
140
  }
250
-
251
141
  ```
252
142
 
253
-
254
-
255
- ```C#
256
-
257
- // Program.cs
258
-
259
-
260
-
261
- using System;
262
-
263
- using System.Windows.Forms;
264
-
265
-
266
-
267
- namespace WindowsFormsApp1
268
-
269
- {
270
-
271
- static class Program
272
-
273
- {
274
-
275
- [STAThread]
276
-
277
- static void Main()
278
-
279
- {
280
-
281
- Application.EnableVisualStyles();
282
-
283
- Application.SetCompatibleTextRenderingDefault(false);
284
-
285
- Application.Run(new Form1());
286
-
287
- }
288
-
289
- }
290
-
291
- }
292
-
293
- ```
294
-
295
-
296
-
297
- --
143
+ ---
298
-
299
-
300
144
 
301
145
  システムファイル等はデフォルトですと削除できず例外となります。
302
-
303
146
  適切にtry catchが必要になります。
304
-
305
147
  テスト中に誤って重要ファイルを消さないように十分注意してください。
306
148
 
307
-
308
-
309
149
  1か月でここまで書けるのは素晴らしいですね(参考コードそのままだったとしてもその調査力と理解力が)