Example #1
0
        /// <summary>
        /// Gets the path from the configuration file based on what line is being processed and a regex pattern.
        /// </summary>
        /// <param name="line">The line to read from the file.</param>
        /// <param name="regex">The regex pattern to use against the line.</param>
        /// <param name="path">The output of the path being returned.</param>
        /// <returns>A boolean to indicate if getting a path was successful or not.</returns>
        private static bool GetPath(string line, string regex, out string path)
        {
            if (line.StartsWith("#") || !Regex.IsMatch(line, regex))
            {
                path = null;
                return(false);
            }

            path = Regex.Match(line, regex).Groups["Path"].Value;

            TagCollection tagCollection = new TagCollection();

            tagCollection.Add(new Tag("user", "The user using this computer (%user%)", TagType.User, active: true));
            tagCollection.Add(new Tag("machine", "The name of the computer (%machine%)", TagType.Machine, active: true));

            path = MacroParser.ParseTags(path, tagCollection);

            if (FileSystem.HasExtension(path))
            {
                string dir = FileSystem.GetDirectoryName(path);

                if (!string.IsNullOrEmpty(dir) && !FileSystem.DirectoryExists(dir))
                {
                    FileSystem.CreateDirectory(dir);
                }
            }
            else
            {
                if (!path.EndsWith(FileSystem.DirectorySeparatorChar().ToString()))
                {
                    path += FileSystem.DirectorySeparatorChar();
                }

                if (!string.IsNullOrEmpty(path) && !FileSystem.DirectoryExists(path))
                {
                    FileSystem.CreateDirectory(path);
                }
            }

            return(true);
        }