This commit is contained in:
tsommer committed 2024-02-17 22:35:45 +01:00
1 parent eff1df2df3
commit c7dda52a49
14 files changed
+284 -93

No files matched your search

+32
View File
@@ -0,0 +1,32 @@
using System.ComponentModel.DataAnnotations;
namespace Terminal.Validation;
public class ValidPathAttribute : ValidationAttribute
{
protected override ValidationResult IsValid(object? value, ValidationContext validationContext)
{
if (value is string path)
{
try
{
path = Environment.ExpandEnvironmentVariables(path);
var info = new FileInfo(path);
if (info.Exists)
return ValidationResult.Success!;
using (var stream = info.Create())
stream.Close();
info.Delete();
return ValidationResult.Success!;
}
catch
{
return new ValidationResult($"The path '{path}' is not valid.");
}
}
return new ValidationResult($"The path '{value}' is not valid.");
}
}