-
Notifications
You must be signed in to change notification settings - Fork 0
/
ValidatePathExists.cs
45 lines (38 loc) · 1.22 KB
/
ValidatePathExists.cs
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
using System;
using System.IO;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
namespace Xdt.Powershell
{
class ValidatePathExists : ValidateEnumeratedArgumentsAttribute
{
public PathType PathType { get; set; }
public ValidatePathExists(PathType pathType = PathType.Any)
{
PathType = pathType;
}
protected override void ValidateElement(object element)
{
var path = (element as FileSystemInfo)?.FullName ?? element.ToString();
var isValid = false;
switch (PathType)
{
case PathType.Any:
isValid = Directory.Exists(path) || File.Exists(path);
break;
case PathType.Leaf:
isValid = File.Exists(path);
break;
case PathType.Directory:
isValid = Directory.Exists(path);
break;
default:
throw new ArgumentOutOfRangeException();
}
if (!isValid)
{
throw new ItemNotFoundException($"{element} was not found.");
}
}
}
}