teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

見直しキャンペーン中

2023/07/21 08:22

投稿

TN8001
TN8001

スコア10108

answer CHANGED
@@ -1,155 +1,149 @@
1
- 最初は実行ファイルにドラッグ&ドロップの場合、画面なしかなと思いましたが出るんですね?
2
-
3
- 戻り値で消したフォルダを返すようにするといいんじゃないでしょうか。
4
-
5
- ```C#
6
- // Form1.cs
7
-
8
- using System;
9
- using System.Collections.Generic;
10
- using System.IO;
11
- using System.Linq;
12
- using System.Windows.Forms;
13
-
14
- namespace WindowsFormsApp1
15
- {
16
- public partial class Form1 : Form
17
- {
18
- public Form1()
19
- {
20
- InitializeComponent();
21
- // デザイナで設定できる プロパティウィンドウ-動作
22
- //this.listBox1.AllowDrop = true;
23
-
24
- string[] files = Environment.GetCommandLineArgs();
25
-
26
- if(files.Length > 1)
27
- {
28
- Process processObj = new Process();
29
- // ↓string[]の仲間 消されたフォルダパス ↓いっこ飛ばす
30
- IEnumerable<string> deleteFolders = processObj.Other_Process(files.Skip(1));
31
- listBox1.Items.AddRange(deleteFolders.ToArray());
32
- }
33
- }
34
-
35
- private void listBox1_DragEnter(object sender, DragEventArgs e)
36
- {
37
- // ダメな場合を先に見てreturnすると{}が減って見やすい
38
- if(!e.Data.GetDataPresent(DataFormats.FileDrop))
39
- {
40
- e.Effect = DragDropEffects.None;
41
- return;
42
- }
43
-
44
- string[] paths = (string[])e.Data.GetData(DataFormats.FileDrop);
45
-
46
- foreach(string path in paths)
47
- {
48
- // 選択にフォルダ以外(ファイル)が入っていたら禁止
49
- if(!Directory.Exists(path))
50
- {
51
- e.Effect = DragDropEffects.None;
52
- return;
53
- }
54
- }
55
-
56
- e.Effect = DragDropEffects.All;
57
- }
58
-
59
- private void listBox1_DragDrop(object sender, DragEventArgs e)
60
- {
61
- string[] selectFolders = (string[])e.Data.GetData(DataFormats.FileDrop, false);
62
-
63
- Process processObj = new Process();
64
-
65
- // ↓string[]の仲間 消されたフォルダパス
66
- IEnumerable<string> deleteFolders = processObj.Other_Process(selectFolders);
67
- listBox1.Items.AddRange(deleteFolders.ToArray());
68
- }
69
- }
70
- }
71
- ```
72
-
73
- ```C#
74
- // Process.cs
75
-
76
- using System.Collections.Generic;
77
- using System.IO;
78
- using System.Linq;
79
-
80
- namespace WindowsFormsApp1
81
- {
82
- public class Process
83
- {
84
- // ↓消したフォルダを戻り値で返すように
85
- public IEnumerable<string> Other_Process(IEnumerable<string> folders)
86
- {
87
- // ↓string[]の仲間 こちらは後から追加できる
88
- List<string> deleteFolders = new List<string>();
89
-
90
- foreach(string folder in folders)
91
- {
92
- string[] subFolders;
93
- try
94
- {
95
- // アクセスできないフォルダの場合例外
96
- subFolders = Directory.GetDirectories(folder);
97
- }
98
- catch
99
- {
100
- // アクセスできないフォルダは無視
101
- continue;
102
- }
103
-
104
- foreach(string subFolder in subFolders)
105
- {
106
- // とりあえず数字が入っているフォルダを削除するつもりになる
107
- if(subFolder.Any(char.IsDigit))
108
- {
109
- //DeleteDirectory(subFolder);
110
- deleteFolders.Add(subFolder);
111
- }
112
- }
113
- }
114
-
115
- return deleteFolders;
116
- }
117
-
118
- private void DeleteDirectory(string dir)
119
- {
120
- // 読み取り専用ファイルがあると例外
121
- // http://dobon.net/vb/dotnet/file/deletedirectory.html
122
- Directory.Delete(dir, true);
123
- }
124
- }
125
- }
126
- ```
127
-
128
- ```C#
129
- // Program.cs
130
-
131
- using System;
132
- using System.Windows.Forms;
133
-
134
- namespace WindowsFormsApp1
135
- {
136
- static class Program
137
- {
138
- [STAThread]
139
- static void Main()
140
- {
141
- Application.EnableVisualStyles();
142
- Application.SetCompatibleTextRenderingDefault(false);
143
- Application.Run(new Form1());
144
- }
145
- }
146
- }
147
- ```
148
-
149
- --
150
-
151
- システムファイル等はデフォルトですと削除できず例外となります。
152
- 適切にtry catchが必要になります。
153
- テスト中に誤って重要ファイルを消さないように十分注意してください。
154
-
1
+ 最初は実行ファイルにドラッグ&ドロップの場合、画面なしかなと思いましたが出るんですね?
2
+
3
+ 戻り値で消したフォルダを返すようにするといいんじゃないでしょうか。
4
+
5
+ ```cs:Form1.cs
6
+ using System;
7
+ using System.Collections.Generic;
8
+ using System.IO;
9
+ using System.Linq;
10
+ using System.Windows.Forms;
11
+
12
+ namespace WindowsFormsApp1
13
+ {
14
+ public partial class Form1 : Form
15
+ {
16
+ public Form1()
17
+ {
18
+ InitializeComponent();
19
+ // デザイナで設定できる プロパティウィンドウ-動作
20
+ //this.listBox1.AllowDrop = true;
21
+
22
+ string[] files = Environment.GetCommandLineArgs();
23
+
24
+ if(files.Length > 1)
25
+ {
26
+ Process processObj = new Process();
27
+ // ↓string[]の仲間 消されたフォルダパス ↓いっこ飛ばす
28
+ IEnumerable<string> deleteFolders = processObj.Other_Process(files.Skip(1));
29
+ listBox1.Items.AddRange(deleteFolders.ToArray());
30
+ }
31
+ }
32
+
33
+ private void listBox1_DragEnter(object sender, DragEventArgs e)
34
+ {
35
+ // ダメな場合を先に見てreturnすると{}が減って見やすい
36
+ if(!e.Data.GetDataPresent(DataFormats.FileDrop))
37
+ {
38
+ e.Effect = DragDropEffects.None;
39
+ return;
40
+ }
41
+
42
+ string[] paths = (string[])e.Data.GetData(DataFormats.FileDrop);
43
+
44
+ foreach(string path in paths)
45
+ {
46
+ // 選択にフォルダ以外(ファイル)が入っていたら禁止
47
+ if(!Directory.Exists(path))
48
+ {
49
+ e.Effect = DragDropEffects.None;
50
+ return;
51
+ }
52
+ }
53
+
54
+ e.Effect = DragDropEffects.All;
55
+ }
56
+
57
+ private void listBox1_DragDrop(object sender, DragEventArgs e)
58
+ {
59
+ string[] selectFolders = (string[])e.Data.GetData(DataFormats.FileDrop, false);
60
+
61
+ Process processObj = new Process();
62
+
63
+ // ↓string[]の仲間 消されたフォルダパス
64
+ IEnumerable<string> deleteFolders = processObj.Other_Process(selectFolders);
65
+ listBox1.Items.AddRange(deleteFolders.ToArray());
66
+ }
67
+ }
68
+ }
69
+ ```
70
+
71
+ ```cs:Process.cs
72
+ using System.Collections.Generic;
73
+ using System.IO;
74
+ using System.Linq;
75
+
76
+ namespace WindowsFormsApp1
77
+ {
78
+ public class Process
79
+ {
80
+ // ↓消したフォルダを戻り値で返すように
81
+ public IEnumerable<string> Other_Process(IEnumerable<string> folders)
82
+ {
83
+ // ↓string[]の仲間 こちらは後から追加できる
84
+ List<string> deleteFolders = new List<string>();
85
+
86
+ foreach(string folder in folders)
87
+ {
88
+ string[] subFolders;
89
+ try
90
+ {
91
+ // アクセスできないフォルダの場合例外
92
+ subFolders = Directory.GetDirectories(folder);
93
+ }
94
+ catch
95
+ {
96
+ // アクセスできないフォルダは無視
97
+ continue;
98
+ }
99
+
100
+ foreach(string subFolder in subFolders)
101
+ {
102
+ // とりあえず数字が入っているフォルダを削除するつもりになる
103
+ if(subFolder.Any(char.IsDigit))
104
+ {
105
+ //DeleteDirectory(subFolder);
106
+ deleteFolders.Add(subFolder);
107
+ }
108
+ }
109
+ }
110
+
111
+ return deleteFolders;
112
+ }
113
+
114
+ private void DeleteDirectory(string dir)
115
+ {
116
+ // 読み取り専用ファイルがあると例外
117
+ // http://dobon.net/vb/dotnet/file/deletedirectory.html
118
+ Directory.Delete(dir, true);
119
+ }
120
+ }
121
+ }
122
+ ```
123
+
124
+ ```cs:Program.cs
125
+ using System;
126
+ using System.Windows.Forms;
127
+
128
+ namespace WindowsFormsApp1
129
+ {
130
+ static class Program
131
+ {
132
+ [STAThread]
133
+ static void Main()
134
+ {
135
+ Application.EnableVisualStyles();
136
+ Application.SetCompatibleTextRenderingDefault(false);
137
+ Application.Run(new Form1());
138
+ }
139
+ }
140
+ }
141
+ ```
142
+
143
+ ---
144
+
145
+ システムファイル等はデフォルトですと削除できず例外となります。
146
+ 適切にtry catchが必要になります。
147
+ テスト中に誤って重要ファイルを消さないように十分注意してください。
148
+
155
149
  1か月でここまで書けるのは素晴らしいですね(参考コードそのままだったとしてもその調査力と理解力が)