using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace RainmeterStudio.Core.Utils
{
public static class PathHelper
{
///
/// Validates a path
///
/// The path
/// True if the path is valid
public static bool IsPathValid(string path)
{
// Check for invalid characters
if (Path.GetInvalidPathChars().Intersect(path).Any())
return false;
return true;
}
///
/// Validates a file name
///
/// Name of file
///
public static bool IsFileNameValid(string name)
{
// No name is not a valid name
if (String.IsNullOrEmpty(name))
return false;
// Check for invalid characters
if (Path.GetInvalidFileNameChars().Intersect(name).Any())
return false;
return true;
}
///
/// Converts an absolute path to a path relative to current working directory
///
/// Absolute path
/// Relative path
public static string GetRelativePath(string path)
{
return GetRelativePath(path, Environment.CurrentDirectory);
}
///
/// Converts an absolute path to a relative path
///
/// Absolute path to file
/// Relative reference
/// Relative path
public static string GetRelativePath(string path, string relativeTo)
{
Uri pathUri = new Uri(path);
// Folder must end in backslash
if (!relativeTo.EndsWith(Path.DirectorySeparatorChar.ToString()))
{
relativeTo += Path.DirectorySeparatorChar;
}
Uri folderUri = new Uri(relativeTo);
Uri relativePath = pathUri.MakeRelativeUri(folderUri);
return Uri.UnescapeDataString(relativeTo.ToString().Replace('/', Path.DirectorySeparatorChar));
}
}
}