Exemple #1
0
        /// <summary>
        /// Normalisiert den Dateinamen und benennt die Datei ggf. um. (Bedingung: endet mit .pdf)
        /// </summary>
        public string NormalizeFile(string path)
        {
            string filename = path.Split('\\').Last();
            string newName  = filename.Substring(0, filename.Length - 4);

            if (namePattern.IsNormalizedInstrument(newName))
            {
                return(path);
            }

            newName = namePattern.NormalizeInstrument(newName);

            if (newName == "") // -> Pattern unbekannt
            {
                WorkList.GetInstance().LoTasks.Add(new Task()
                {
                    Type = TaskType.FileNamePattern,
                    Path = path
                });

                return(path);
            }
            else
            {
                string newPath = path.Substring(0, path.Length - filename.Length) + newName + ".pdf";

                File.Move(
                    Path.Combine(config.StoragePath, path),
                    Path.Combine(config.StoragePath, newPath)
                    ); // todo: Exceptionhandling fehlt: Was passiert, wenn neuer Pfad schon vorhanden?

                return(newPath);
            }
        }
        /// <summary>
        /// Überprüft, ob der Name ein valider Dateiname ist.
        /// </summary>
        public static bool IsValidFilename(string filename)
        {
            if (filename == "" || !filename.ToLower().EndsWith(".pdf"))
            {
                return(false);
            }

            NamePattern namePattern = Factory.GetNamePattern();

            if (!namePattern.IsNormalizedInstrument(filename))
            {
                return(false);
            }

            filename = filename.Split('\\').Last();
            filename = filename.Substring(0, filename.Length - 4);

            string[] result = filename.Split('#');

            if (result.Length == 3)
            {
                try
                {
                    Convert.ToInt32(result[2]);
                    return(true);
                }
                catch
                {
                    return(false);
                }
            }

            if (result.Length <= 3)
            {
                return(true);
            }

            return(false);
        }