回答編集履歴

1

追記

2019/03/15 03:33

投稿

Zuishin
Zuishin

スコア28660

test CHANGED
@@ -9,3 +9,157 @@
9
9
 
10
10
 
11
11
  変更は [Dictionary<TKey,TValue> Class](https://docs.microsoft.com/ja-jp/dotnet/api/system.collections.generic.dictionary-2?view=netframework-4.7.2) を使うと良いでしょう。まずは片方のリストを、ファイル名をキーに辞書に入れ、次のリストに含まれる項目の一つ一つに対してその辞書に同じファイル名の項目が入っているかどうかを調べます。入っていた場合にはその二つの項目を比較し、更新日時が違えば変更されたことがわかります。
12
+
13
+
14
+
15
+ # 追記
16
+
17
+
18
+
19
+ こんな感じになります。
20
+
21
+
22
+
23
+ ```C#
24
+
25
+ using System;
26
+
27
+ using System.Collections.Generic;
28
+
29
+ using System.IO;
30
+
31
+ using System.Linq;
32
+
33
+
34
+
35
+ namespace Etude
36
+
37
+ {
38
+
39
+ class Program
40
+
41
+ {
42
+
43
+ static void Main(string[] args)
44
+
45
+ {
46
+
47
+ string fileMask = "*.txt";
48
+
49
+ foreach (var file in Directory.GetFiles(".", fileMask))
50
+
51
+ {
52
+
53
+ File.Delete(file);
54
+
55
+ }
56
+
57
+
58
+
59
+ File.Create("a.txt").Close();
60
+
61
+ File.Create("b.txt").Close();
62
+
63
+ File.Create("c.txt").Close();
64
+
65
+ var dir = new DirectoryInfo(".");
66
+
67
+ var before = dir
68
+
69
+ .GetFiles(fileMask)
70
+
71
+ .ToDictionary(a => a.FullName);
72
+
73
+ Dump(nameof(before), before.Keys);
74
+
75
+
76
+
77
+ File.Create("d.txt").Close();
78
+
79
+ File.Delete("b.txt");
80
+
81
+ File.Delete("c.txt");
82
+
83
+ File.Create("c.txt").Close();
84
+
85
+ var after = dir
86
+
87
+ .GetFiles(fileMask)
88
+
89
+ .ToDictionary(a => a.FullName);
90
+
91
+ Dump(nameof(after), after.Keys);
92
+
93
+
94
+
95
+ var added = after
96
+
97
+ .Keys
98
+
99
+ .Except(before.Keys)
100
+
101
+ .Select(a => after[a])
102
+
103
+ .ToArray();
104
+
105
+ Dump(nameof(added), added.Select(a => a.FullName));
106
+
107
+
108
+
109
+ var removed = before
110
+
111
+ .Keys
112
+
113
+ .Except(after.Keys)
114
+
115
+ .Select(a => before[a])
116
+
117
+ .ToArray();
118
+
119
+ Dump(nameof(removed), removed.Select(a => a.FullName));
120
+
121
+
122
+
123
+ var changed = before
124
+
125
+ .Keys
126
+
127
+ .Where(a => after.ContainsKey(a))
128
+
129
+ .Where(a => after[a].LastWriteTime != before[a].LastWriteTime)
130
+
131
+ .ToArray();
132
+
133
+ Dump(nameof(changed), changed);
134
+
135
+
136
+
137
+ Console.ReadKey();
138
+
139
+ }
140
+
141
+
142
+
143
+ static void Dump(string title, IEnumerable<string> files)
144
+
145
+ {
146
+
147
+ Console.WriteLine($"[{title}]");
148
+
149
+ foreach (var file in files)
150
+
151
+ {
152
+
153
+ Console.WriteLine(file);
154
+
155
+ }
156
+
157
+ Console.WriteLine();
158
+
159
+ }
160
+
161
+ }
162
+
163
+ }
164
+
165
+ ```