public static string ValidateAbsoluteFilePath()

in Configurator/Base/Classes/Utilities.cs [1646:1708]


    public static string ValidateAbsoluteFilePath(string path, bool validatePathIsEmpty)
    {
      // Check if the path has the required minimum length.
      if (string.IsNullOrEmpty(path)
          || path.Length < 3)
      {
        return Resources.PathInvalidMinimumLengthError;
      }

      // Check if the path contains invalid characters.
      var invalidFileNameChars = new string(Path.GetInvalidPathChars());
      invalidFileNameChars += @":/?*" + "\"";
      var containsBadCharacter = new Regex("[" + Regex.Escape(invalidFileNameChars) + "]");
      if (containsBadCharacter.IsMatch(path.Substring(3, path.Length - 3)))
      {
        return Resources.PathInvalidBadCharactersError;
      }

      // Check if the path is rooted.
      var directoryPath = Path.GetDirectoryName(path);
      if (!string.IsNullOrEmpty(directoryPath)
          && !Path.IsPathRooted(path))
      {
        return Resources.PathInvalidNotAbsoluteError;
      }

      // Check if the path starts with the expected format.
      var driveRegex = new Regex(@"^[a-zA-Z]:\\$");
      if (!driveRegex.IsMatch(path.Substring(0, 3)))
      {
        return Resources.PathInvalidFormatError;
      }

      // Check if the drive exists.
      try
      {
        var machineDrives = DriveInfo.GetDrives().Select(drive => drive.Name);
        if (!machineDrives.Contains(path.Substring(0, 3)))
        {
          return Resources.PathInvalidDriveNotFoundError;
        }
      }
      catch (Exception)
      {
        return Resources.PathInvalidUnableToGetDrivesError;
      }

      // Check that path does not end with a . char.
      if (path[path.Length - 1] == '.')
      {
        return Resources.PathInvalidEndsInDotError;
      }

      if (validatePathIsEmpty
          && Directory.Exists(path)
          && (Directory.GetFiles(path).Length > 0
              || Directory.GetDirectories(path).Length > 0))
      {
        return Resources.PathExistsAndIsNotEmpty;
      }

      return string.Empty;
    }