mirror of
https://github.com/Tichau/FileConverter.git
synced 2026-11-19 17:12:22 +01:00
49 lines
1.8 KiB
C#
49 lines
1.8 KiB
C#
// <copyright file="PathHelpers.cs" company="AAllard">License: http://www.gnu.org/licenses/gpl.html GPL version 3.</copyright>
|
|
|
|
namespace FileConverter
|
|
{
|
|
using System.Collections.Generic;
|
|
using System.Text.RegularExpressions;
|
|
|
|
public static class PathHelpers
|
|
{
|
|
private static Regex driveLetterRegex = new Regex(@"[a-zA-Z]:\\");
|
|
private static Regex pathRegex = new Regex(@"^[a-zA-Z]:\\(?:[^\\/:*?""<>|\r\n]+\\)*[^\.\\/:*?""<>|\r\n][^\\/:*?""<>|\r\n]*$");
|
|
private static Regex filenameRegex = new Regex(@"[^\\]*", RegexOptions.RightToLeft);
|
|
private static Regex directoryRegex = new Regex(@"(?:([^\\]*)\\)*");
|
|
|
|
public static bool IsPathDriveLetterValid(string path)
|
|
{
|
|
return PathHelpers.driveLetterRegex.IsMatch(path);
|
|
}
|
|
|
|
public static bool IsPathValid(string path)
|
|
{
|
|
return PathHelpers.pathRegex.IsMatch(path);
|
|
}
|
|
|
|
public static string GetFileName(string path)
|
|
{
|
|
MatchCollection matchCollection = PathHelpers.filenameRegex.Matches(path);
|
|
Match filenameMatch = matchCollection.Count > 0 ? matchCollection[0] : null;
|
|
return filenameMatch?.Groups[0].Value;
|
|
}
|
|
|
|
public static IEnumerable<string> GetDirectories(string path)
|
|
{
|
|
MatchCollection matchCollection = PathHelpers.directoryRegex.Matches(path);
|
|
Match match = matchCollection.Count > 0 ? matchCollection[0] : null;
|
|
if (match == null || match.Groups.Count < 2)
|
|
{
|
|
yield break;
|
|
}
|
|
|
|
Group matchGroup = match.Groups[1];
|
|
for (int index = 0; index < matchGroup.Captures.Count; index++)
|
|
{
|
|
yield return matchGroup.Captures[index].Value;
|
|
}
|
|
}
|
|
}
|
|
}
|