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
|
using System;
using System.Collections.Generic;
namespace AdString.IO
{
using System.IO;
public static class IOExtensions
{
public enum IOType { File, Directory, DontExist };
/// <summary>
/// Gibt die Endung einer Datei zurück
/// </summary>
/// <param name="__path">Pfad der Datei</param>
/// <returns>Endung ohne Punkt</returns>
public static string _Extenstion(this String __path)
{
if (File.Exists(__path))
return new FileInfo(__path).Extension.Substring(1);
else
throw new FileNotFoundException("Datei existiert nicht.");
}
/// <summary>
/// Gibt die Dateigröße zurück
/// </summary>
/// <param name="__path">Pfad der Datei</param>
/// <returns>Dateigröße</returns>
public static long _FileSize(this String __path)
{
if(File.Exists(__path))
return new FileInfo(__path).Length;
else
throw new FileNotFoundException("Datei existiert nicht.");
}
/// <summary>
/// Gibt die Ordnergröße zurück
/// </summary>
/// <param name="__path">Pfad zum Ordner</param>
/// <returns>Ordnergröße</returns>
public static long _FolderSize(this String __path)
{
return __path._FolderSize(false);
}
/// <summary>
/// Gibt die Ordnergröße zurück
/// </summary>
/// <param name="__path">Pfad zum Ordner</param>
/// <param name="__recursiv">Unterordner auch dazu zählen</param>
/// <returns>Ordnergröße</returns>
public static long _FolderSize(this String __path, bool __recursiv)
{
if (Directory.Exists(__path))
{
if (__recursiv)
return FolderSize(new DirectoryInfo(__path));
else
{
long retValue = 0;
foreach (FileInfo fi in new DirectoryInfo(__path).GetFiles())
retValue += fi.Length;
return retValue;
}
}
else
throw new DirectoryNotFoundException("Ordner existiert nicht.");
}
/// <summary>
/// Erzeigt aus einem Pfad ein FileInfo
/// </summary>
/// <param name="__path">Pfad</param>
/// <returns>FileInfo</returns>
public static FileInfo _ConvertToFileInfo(this String __path)
{
if (File.Exists(__path))
return new FileInfo(__path);
else
throw new FileNotFoundException("Datei existiert nicht.");
}
/// <summary>
/// Erzeugt aus einem Pfad ein DirectoryInfo
/// </summary>
/// <param name="__path">Pfad</param>
/// <returns>DirectoryInfo</returns>
public static DirectoryInfo _ConvertToDirectoryInfo(this String __path)
{
if (Directory.Exists(__path))
return new DirectoryInfo(__path);
else
throw new DirectoryNotFoundException("Ordner existiert nicht.");
}
/// <summary>
/// Ermittelt ob der Pfad zu eine Datei oder zu einem Ordner zeigt
/// </summary>
/// <param name="__path">Pfad</param>
/// <returns>Datei/Ordner/Datei existiert nicht</returns>
public static IOType _Type(this String __path)
{
if (File.Exists(__path))
return IOType.File;
else if (Directory.Exists(__path))
return IOType.Directory;
return IOType.DontExist;
}
/// <summary>
/// Letzter Schreibzugriff auf den Pfad
/// </summary>
/// <param name="__path">Pfad</param>
/// <returns>Zeit</returns>
public static DateTime _LastWriteTime(this String __path)
{
if (File.Exists(__path))
return new FileInfo(__path).LastWriteTime;
else if (Directory.Exists(__path))
return new DirectoryInfo(__path).LastWriteTime;
else
throw new IOException("Kein Ziel zu diesem Pfad Vorhanden");
}
/// <summary>
/// Letzter Lesezugriff auf den Pfad
/// </summary>
/// <param name="__path">Pfad</param>
/// <returns>Zeit</returns>
public static DateTime _LastAccessTime(this String __path)
{
if (File.Exists(__path))
return new FileInfo(__path).LastAccessTime;
else if (Directory.Exists(__path))
return new DirectoryInfo(__path).LastAccessTime;
else
throw new IOException("Kein Ziel zu diesem Pfad Vorhanden");
}
/// <summary>
/// Zeit an dem die Datei/der Ordner erstellt wurde
/// </summary>
/// <param name="__path">Pfad</param>
/// <returns>Zeit</returns>
public static DateTime _CreationTime(this String __path)
{
if (File.Exists(__path))
return new FileInfo(__path).CreationTime;
else if (Directory.Exists(__path))
return new DirectoryInfo(__path).CreationTime;
else
throw new IOException("Kein Ziel zu diesem Pfad Vorhanden");
}
/// <summary>
/// Gibt den Übergeordneten Ordner an
/// </summary>
/// <param name="__path">Pfad</param>
/// <returns>Übergeordneter Ordner</returns>
public static string _Directory(this String __path)
{
if (File.Exists(__path))
return new FileInfo(__path).DirectoryName;
else if (Directory.Exists(__path))
return new DirectoryInfo(__path).Parent.Name;
else
throw new IOException("Kein Ziel zu diesem Pfad Vorhanden");
}
/// <summary>
/// Liste mit untergeordneten Dateien
/// </summary>
/// <param name="__pfad">Pfad des Ordners</param>
/// <returns>Liste</returns>
public static List<string> _ListFiles(this String __path)
{
List<string> retValue = new List<string>();
if (!Directory.Exists(__path))
throw new DirectoryNotFoundException("Ordner existiert nicht.");
foreach (FileInfo fi in new DirectoryInfo(__path).GetFiles())
retValue.Add(fi.FullName);
return retValue;
}
/// <summary>
/// Liste mit untergeordneten Ordner
/// </summary>
/// <param name="__pfad">Pfad des Ordners</param>
/// <returns>Liste</returns>
public static List<string> _ListDirectories(this String __path)
{
List<string> retValue = new List<string>();
if (!Directory.Exists(__path))
throw new DirectoryNotFoundException("Ordner existiert nicht.");
foreach (DirectoryInfo di in new DirectoryInfo(__path).GetDirectories())
retValue.Add(di.FullName);
return retValue;
}
#region Hilfmethoden
/// <summary>
/// Ermittelt die Ordnergröße samt Unterordner
/// </summary>
/// <param name="__di">Ordner</param>
/// <returns>Größe</returns>
private static long FolderSize(DirectoryInfo __di)
{
long retValue = 0;
foreach (DirectoryInfo di in __di.GetDirectories())
retValue += FolderSize(di);
foreach (FileInfo fi in __di.GetFiles())
retValue += fi.Length;
return retValue;
}
#endregion
}
}
|