using System.IO;
namespace AppLimit.CloudComputing.SharpBox.Common.IO
{
///
/// A class with reimplements some aspects if the Path clas
/// to make the handling of path names easier
///
public class PathHelper
{
private readonly string _path;
///
/// The used path delimiter
///
public const char Delimiter = '/';
///
/// ctor with access path
///
///
public PathHelper(string path)
{
_path = path;
}
///
/// Checks if the given path is rooted
///
///
public bool IsPathRooted()
{
return _path.Length != 0 && _path[0] == Delimiter;
}
///
/// Returns all path elements in a path
///
///
public string[] GetPathElements()
{
// remove heading and trailing /
var workingPath = IsPathRooted() ? _path.Remove(0, 1) : _path;
workingPath = workingPath.TrimEnd(Delimiter);
return workingPath.Length == 0 ? new string[0] : workingPath.Split(Delimiter);
}
///
/// Returns the directory name
///
///
public string GetDirectoryName()
{
var idx = _path.LastIndexOf(Delimiter);
return idx == 0 ? "" : _path.Substring(0, idx);
}
///
/// Returns the filename
///
///
public string GetFileName()
{
return Path.GetFileName(_path);
}
///
/// Combines several path elements
///
///
///
///
public static string Combine(string left, string right)
{
// remove delimiter
right = right.TrimStart(Delimiter);
left = left.TrimEnd(Delimiter);
// build the path
if (right.Length == 0)
return left;
return left + Delimiter + right;
}
}
}