32 lines
956 B
C#
32 lines
956 B
C#
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.");
|
|
}
|
|
} |