Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 70 lines (59 sloc) 1.919 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
using System;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;

namespace MiscUtils
{
[Serializable]
    public class PathString : StringEquivalent<PathString>
    {
        public PathString(string pathString) : base(NormalizePath(pathString))
        {
        }

        protected PathString(SerializationInfo info, StreamingContext context) : base(info)
        {
        }

        // So the XmlSerializer can create this.
        protected PathString()
        {
        }

        public override PathString Combine(string path)
        {
            Ensure.ArgumentNotNull(path, "path");

            if (path.StartsWith(@"\", StringComparison.Ordinal))
            {
                path = path.Substring(1);
            }

            try
            {
                return Path.Combine(Value, path);
            }
            catch (ArgumentException)
            {
                return AppendPath(Value, path);
            }
        }

        private static string AppendPath(string source, string addition)
        {
            if (source.Last() != Path.DirectorySeparatorChar) source += "/";
            if (addition.First() == '/') addition = addition.Substring(1);
            return source + addition;
        }

        public static implicit operator PathString(string value)
        {
            return value == null ? null : new PathString(value);
        }

        public static implicit operator string(PathString path)
        {
            return path == null ? null : path.Value;
        }

        private static string NormalizePath(string path)
        {
            return path == null ? null : path.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
        }

        public override bool Equals(string other)
        {
            return other != null && Value.Equals(other, StringComparison.OrdinalIgnoreCase);
        }
    }
}
Something went wrong with that request. Please try again.