示例#1
0
        /// <summary>
        /// Gets the common path of both local and remote directories.
        /// </summary>
        /// <returns>
        /// The common path, using forward slashes ( / )
        /// </returns>
        /// <param name='p'>
        /// The full path to be 'shortened'
        /// </param>
        /// <param name='fromLocal'>
        /// True if the given path is in local format.
        /// </param>
        public string GetCommonPath(string p, bool fromLocal)
        {
            if (!fromLocal)
            {
                // Remove the remote path from the begining
                if (this.Paths.Remote != null && p.StartsWith(this.Paths.Remote))
                {
                    if (p.StartsWithButNotEqual(this.Paths.Remote))
                    {
                        p = p.Substring(this.Paths.Remote.Length);
                    }
                }
                // If path starts with homepath instead, remove the home path from the begining
                else if (HomePath != String.Empty && !HomePath.Equals("/"))
                {
                    if (p.StartsWithButNotEqual(HomePath) || p.StartsWithButNotEqual(HomePath.RemoveSlashes()) || p.RemoveSlashes().StartsWithButNotEqual(HomePath))
                    {
                        p = p.Substring(HomePath.Length + 1);
                    }
                    // ... and then remove the remote path
                    if (this.Paths.Remote != null && p.StartsWithButNotEqual(this.Paths.Remote))
                    {
                        p = p.Substring(this.Paths.Remote.Length);
                    }
                }
            }
            if (fromLocal || File.Exists(p) || Directory.Exists(p))
            {
                if (p.Equals(this.Paths.Local))
                {
                    return(".");
                }

                if (!String.IsNullOrWhiteSpace(this.Paths.Local) && p.StartsWith(this.Paths.Local))
                {
                    p = p.Substring(this.Paths.Local.Length);
                    p.ReplaceSlashes();
                }
            }

            p = p.RemoveSlashes();
            if (p.StartsWithButNotEqual("/"))
            {
                p = p.Substring(1);
            }
            if (p.StartsWith("./"))
            {
                p = p.Substring(2);
            }

            if (String.IsNullOrWhiteSpace(p))
            {
                p = "/";
            }

            return(p.ReplaceSlashes());
        }