Ejemplo n.º 1
0
        private string CleanTitle(string title)
        {
            if (string.IsNullOrWhiteSpace(title))
            {
                return("");
            }

            return(FolderUtils.ReplaceChars(title.Trim(), Path.GetInvalidFileNameChars()));
        }
Ejemplo n.º 2
0
        private string GetFullFilePath(int diagramIndex = 0, bool overwriteFiles = true)
        {
            string outputPath     = OutputPath;
            string filenameFormat = SelectedFilenameFormat;

            string filePath = "";

            for (int i = 0; i < SelectedFilenameFormat.Length; i++)
            {
                if (SelectedFilenameFormat[i] != '%')
                {
                    filePath += SelectedFilenameFormat[i];
                }
                else
                {
                    if (i + 1 < SelectedFilenameFormat.Length)
                    {
                        i++; // step forward to look at the next character
                        char nextChar = SelectedFilenameFormat[i];
                        switch (nextChar)
                        {
                        case 't':
                            filePath += CleanTitle(DiagramsToExport[diagramIndex].Title);
                            break;

                        case 'c':
                            filePath += CollectionName;
                            break;

                        case 'h':
                            filePath += DiagramsToExport[diagramIndex].TotalHeight.ToString();
                            break;

                        case 'w':
                            filePath += DiagramsToExport[diagramIndex].TotalWidth.ToString();
                            break;

                        case '0':
                            filePath += diagramIndex.ToString();
                            break;

                        case '1':
                            filePath += (diagramIndex + 1).ToString();
                            break;

                        case '#':
                            filePath += DiagramsToExport.Count.ToString();
                            break;

                        case 'x':
                            filePath += ExportFormat.ToString().ToLower();
                            break;

                        case 'X':
                            filePath += ExportFormat.ToString().ToUpper();
                            break;

                        case '%':
                            filePath += "%";
                            break;

                        default:
                            //i--; // back up so we don't miss the next character
                            break;
                        }
                    }
                }
            }

            string rawFileName = FolderUtils.CleanPath(Path.Combine(outputPath, filePath));

            string folder    = Path.GetDirectoryName(rawFileName);
            string fileName  = Path.GetFileNameWithoutExtension(rawFileName);
            string extension = Path.GetExtension(rawFileName);

            string testfileName = Path.Combine(folder, fileName + extension);

            int  attempt       = 1;
            bool foundFilename = false;

            while (!foundFilename)
            {
                bool fileAlreadyExists     = File.Exists(testfileName);
                bool fileCreatedThisExport = _createdFiles.Exists(createdFile => createdFile.Equals(testfileName, StringComparison.CurrentCultureIgnoreCase));

                if (!fileAlreadyExists || (fileAlreadyExists && !fileCreatedThisExport && overwriteFiles))
                {
                    foundFilename = true;
                }
                else
                {
                    testfileName = Path.Combine(folder, fileName + " (" + attempt + ")" + extension);
                    attempt++;
                }
            }

            return(testfileName);
        }