UrlPath
using System.IO;
namespace System.Configuration
{
internal static class UrlPath
{
private const string FileUrlLocal = "file:///";
private const string FileUrlUnc = "file:";
internal static string GetDirectoryOrRootName(string path)
{
return Path.GetDirectoryName(path) ?? Path.GetPathRoot(path);
}
internal static bool IsEqualOrSubdirectory(string dir, string subdir)
{
if (string.IsNullOrEmpty(dir))
return true;
if (string.IsNullOrEmpty(subdir))
return false;
int num = dir.Length;
if (dir[num - 1] == '\\')
num--;
int num2 = subdir.Length;
if (subdir[num2 - 1] == '\\')
num2--;
if (num2 < num)
return false;
if (string.Compare(dir, 0, subdir, 0, num, StringComparison.OrdinalIgnoreCase) != 0)
return false;
if (num2 > num)
return subdir[num] == '\\';
return true;
}
internal static bool IsEqualOrSubpath(string path, string subpath)
{
return IsEqualOrSubpathImpl(path, subpath, false);
}
internal static bool IsSubpath(string path, string subpath)
{
return IsEqualOrSubpathImpl(path, subpath, true);
}
private static bool IsEqualOrSubpathImpl(string path, string subpath, bool excludeEqual)
{
if (string.IsNullOrEmpty(path))
return true;
if (string.IsNullOrEmpty(subpath))
return false;
int num = path.Length;
if (path[num - 1] == '/')
num--;
int num2 = subpath.Length;
if (subpath[num2 - 1] == '/')
num2--;
if (num2 < num)
return false;
if (excludeEqual && num2 == num)
return false;
if (string.Compare(path, 0, subpath, 0, num, StringComparison.OrdinalIgnoreCase) != 0)
return false;
if (num2 > num)
return subpath[num] == '/';
return true;
}
private static bool IsDirectorySeparatorChar(char ch)
{
if (ch != '\\')
return ch == '/';
return true;
}
private static bool IsAbsoluteLocalPhysicalPath(string path)
{
if (path == null || path.Length < 3)
return false;
if (path[1] == ':')
return IsDirectorySeparatorChar(path[2]);
return false;
}
private static bool IsAbsoluteUncPhysicalPath(string path)
{
if (path == null || path.Length < 3)
return false;
if (IsDirectorySeparatorChar(path[0]))
return IsDirectorySeparatorChar(path[1]);
return false;
}
internal static string ConvertFileNameToUrl(string fileName)
{
string str;
if (IsAbsoluteLocalPhysicalPath(fileName))
str = "file:///";
else {
if (!IsAbsoluteUncPhysicalPath(fileName))
throw ExceptionUtil.ParameterInvalid("fileName");
str = "file:";
}
return str + fileName.Replace('\\', '/');
}
}
}