Initial
This commit is contained in:
14 files changed
+284
-93
No files matched your search
@@ -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.");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user