private string Validate(string propertyName) { // Return error message if there is an error, else return empty or null string. switch (propertyName) { case "Name": { if (string.IsNullOrEmpty(this.Name)) { return("The preset name can't be empty."); } if (this.Name.Contains(";")) { return("The preset name can't contains the character ';'."); } Application application = Application.Current as Application; int? count = application?.Settings?.ConversionPresets?.Count(match => match?.name == this.Name); if (count > 1) { return("The preset name is already used."); } } break; case "OutputFileNameTemplate": { string sampleOutputFilePath = this.GenerateOutputFilePath(FileConverter.Properties.Resources.OuputFileNameTemplateSample, 1, 3); if (string.IsNullOrEmpty(sampleOutputFilePath)) { return("The output filename template must produce a non empty result."); } if (!PathHelpers.IsPathValid(sampleOutputFilePath)) { // Diagnostic to feedback purpose. // Drive letter. if (!PathHelpers.IsPathDriveLetterValid(sampleOutputFilePath)) { return("The output filename template must define a root (for example c:\\, use (p) to use the input file path)."); } // File name. string filename = PathHelpers.GetFileName(sampleOutputFilePath); if (filename == null) { return("The output file name must not be empty (use (f) to use the name of the input file)."); } char[] invalidFileNameChars = System.IO.Path.GetInvalidFileNameChars(); for (int index = 0; index < invalidFileNameChars.Length; index++) { if (filename.Contains(invalidFileNameChars[index])) { return("The output file name must not contains the character '" + invalidFileNameChars[index] + "'."); } } // Directory names. string path = sampleOutputFilePath.Substring(3, sampleOutputFilePath.Length - 3 - filename.Length); char[] invalidPathChars = System.IO.Path.GetInvalidPathChars(); for (int index = 0; index < invalidPathChars.Length; index++) { if (string.IsNullOrEmpty(path)) { return("The output directory name must not be empty (use (d0), (d1), ... to use the name of the parent directories of the input file)."); } if (path.Contains(invalidPathChars[index])) { return("The output directory name must not contains the character '" + invalidPathChars[index] + "'."); } } string[] directories = path.Split('\\'); for (int index = 0; index < directories.Length; ++index) { string directoryName = directories[index]; if (string.IsNullOrEmpty(directoryName)) { return("The output directory name must not be empty (use (d0), (d1), ... to use the name of the parent directories of the input file)."); } } return("The output filename template is invalid"); } } break; } return(string.Empty); }
public string GenerateOutputFilePath(string inputFilePath, int numberIndex, int numberMax) { return(PathHelpers.GenerateFilePathFromTemplate(inputFilePath, this.OutputType, this.OutputFileNameTemplate, numberIndex, numberMax)); }