示例#1
0
        public static string AbsolutePath(string path)
        {
            if (string.IsNullOrEmpty(path) || Path.IsPathRooted(path)) return path;

               ///////// EDITED BY LMT 27/NOV/2008 for NHT-11
               string reason;
               if (!PathHelper.IsValidRelativePath(path, out reason))
               {
               if (!path.StartsWith(@".\"))
               {
                   path = @".\" + path;
                   if (!PathHelper.IsValidRelativePath(path, out reason))
                       throw new ArgumentException(string.Format("{0}. {1}", path, reason));
               }
               else
                   throw new ArgumentException(string.Format("{0}. {1}", path, reason));
               }
               FilePathRelative relativePath = new FilePathRelative(path);
               ///////// END EDITED BY LMT 27/NOV/2008 for NHT-11

               DirectoryPathAbsolute absolutePath = new DirectoryPathAbsolute(AppDomain.CurrentDomain.BaseDirectory);
               if (!relativePath.CanGetAbsolutePathFrom(absolutePath))
               throw new ArgumentException(string.Format("Invalid path, " + path));
               return relativePath.GetAbsolutePathFrom(absolutePath).Path;
        }
示例#2
0
 public bool CanGetAbsolutePathFrom(DirectoryPathAbsolute path)
 {
     try {
         this.GetAbsolutePathFrom(path);
         return(true);
     } catch { }
     return(false);
 }
示例#3
0
        //----------------------------------------------
        //
        //   Try Rebase path
        //
        //----------------------------------------------
        // originalPath "A:\X1\X2\X3"  validPath "B:\Y1\X1"  result "B:\Y1\X1\X2\X3"   deeperCommonDirName ="X1"
        // originalPath "A:\X1\X2\X3"  validPath "B:\Y1\Y2"  result null               deeperCommonDirName =null
        // originalPath "A:\X1\X2\X3"  validPath "B:\X1\X2"  result "B:\X1\X2\X3"      deeperCommonDirName ="X2
        // originalPath "A:\X1\X2\X3"  validPath "B:\X2\X3"  result "B:\X2\X3"         deeperCommonDirName ="X3"
        // originalPath "A:\X1\X2\X3"  validPath "B:\X2"     result "B:\X2\X3"         deeperCommonDirName ="X2"
        // originalPath "A:\X1\X2\X3"  validPath "B:\X3\X2"  result "B:\X3\X2\X3"      deeperCommonDirName ="X2"
        // originalPath "A:\X1\X2\X3"  validPath "B:\X3\Y1"  result "B:\X3"            deeperCommonDirName ="X3"
        // originalPath "A:\X1\X2\X3"  validPath "A:\"       result null               deeperCommonDirName =null
        // originalPath "A:\X1\X2\X3"  validPath "A:\Y1"     result null               deeperCommonDirName =null
        // Algo:
        // 1) Find all common dir name between originalPath and validPath
        // 2) If no common dir name then return null, can't guess the path
        // 3) Get the deeper common dir name (deeper in validPath)
        // 4) Return Path(deeperCommonDirName)+ Pathes in originalPath after deeperCommonDirName
        public static bool TryRebasePath(
            DirectoryPathAbsolute originalPath,
            DirectoryPathAbsolute validPath,
            out DirectoryPathAbsolute rebasedPath)
        {
            rebasedPath = DirectoryPathAbsolute.Empty;
            if (PathHelper.IsNullOrEmpty(originalPath))
            {
                return(false);
            }
            if (PathHelper.IsNullOrEmpty(validPath))
            {
                return(false);
            }
            List <string> originalPathDirs = new List <string>(originalPath.Path.Split(Path.DirectorySeparatorChar));
            List <string> validPathDirs    = new List <string>(validPath.Path.Split(Path.DirectorySeparatorChar));

            int indexInValidPathOfDeeperCommonDirName    = -1;
            int indexInOriginalPathOfDeeperCommonDirName = -1;

            // We begin at 1 both loop to avoid Driver in pathes
            for (int validPathIndex = 1; validPathIndex < validPathDirs.Count; validPathIndex++)
            {
                for (int originalPathIndex = 1; originalPathIndex < originalPathDirs.Count; originalPathIndex++)
                {
                    if (validPathDirs[validPathIndex].Length > 0 && // Avoid comparison of empty string that can happen if DirectorySeparatorChar at the end of pathes
                        string.Compare(validPathDirs[validPathIndex], originalPathDirs[originalPathIndex], true) == 0)
                    {
                        indexInValidPathOfDeeperCommonDirName    = validPathIndex;
                        indexInOriginalPathOfDeeperCommonDirName = originalPathIndex;
                        break;
                    }
                }
            }
            if (indexInValidPathOfDeeperCommonDirName == -1)
            {
                // No common dir name, return null
                return(false);
            }

            // Concate Path(deeperCommonDirName)+ Pathes in originalPath after deeperCommonDirName
            Debug.Assert(indexInOriginalPathOfDeeperCommonDirName >= 0);
            List <string> inferedDirNames = validPathDirs.GetRange(0, indexInValidPathOfDeeperCommonDirName);

            inferedDirNames.AddRange(
                originalPathDirs.GetRange(indexInOriginalPathOfDeeperCommonDirName, originalPathDirs.Count - indexInOriginalPathOfDeeperCommonDirName));

            string[] arrayInferedDirNames = new string[inferedDirNames.Count];
            inferedDirNames.CopyTo(arrayInferedDirNames);
            string inferedPath = string.Join(Path.DirectorySeparatorChar.ToString(), arrayInferedDirNames);

            rebasedPath = new DirectoryPathAbsolute(inferedPath);
            return(true);
        }
 //
 //  Absolute/Relative path conversion
 //
 public DirectoryPathAbsolute GetAbsolutePathFrom(DirectoryPathAbsolute path)
 {
     if (path == null)
     {
         throw new ArgumentNullException();
     }
     if (PathHelper.IsEmpty(this) || PathHelper.IsEmpty(path))
     {
         throw new ArgumentException("Cannot compute an absolute path from an empty path.");
     }
     return(new DirectoryPathAbsolute(BasePath.GetAbsolutePathFrom(path, this)));
 }
示例#5
0
        //
        //  Absolute/Relative path conversion
        //
        public FilePathAbsolute GetAbsolutePathFrom(DirectoryPathAbsolute path)
        {
            if (path == null)
            {
                throw new ArgumentNullException();
            }
            if (PathHelper.IsEmpty(this) || PathHelper.IsEmpty(path))
            {
                throw new ArgumentException("Cannot compute an absolute path from an empty path.");
            }
            string pathAbsolute = BasePath.GetAbsolutePathFrom(path, this);

            return(new FilePathAbsolute(pathAbsolute + System.IO.Path.DirectorySeparatorChar + this.FileName));
        }
示例#6
0
        public bool IsChildDirectoryOf(DirectoryPathAbsolute parentDir)
        {
            if (parentDir == null)
            {
                throw new ArgumentNullException("parentDir");
            }
            if (parentDir.IsEmpty)
            {
                throw new ArgumentException("Empty parentDir not accepted", "parentDir");
            }
            string parentPathUpperCase = parentDir.Path.ToUpper();
            string thisPathUpperCase   = this.Path.ToUpper();

            return(thisPathUpperCase.Contains(parentPathUpperCase));
        }
示例#7
0
        public static string RelativePath(string absolutePath)
        {
            Check.Require(!string.IsNullOrEmpty(absolutePath), "absolutePath must not be null or empty.");

            if (!Path.IsPathRooted(absolutePath))
            {
                throw new ArgumentException(absolutePath + " must be an absolute path.");
            }

            DirectoryPathAbsolute baseDirectory =
                new DirectoryPathAbsolute(AppDomain.CurrentDomain.BaseDirectory);

            DirectoryPathAbsolute directory =
                new DirectoryPathAbsolute(absolutePath);

            return(directory.GetPathRelativeFrom(baseDirectory).Path);
        }
示例#8
0
        protected static string GetAbsolutePathFrom(DirectoryPathAbsolute pathFrom, BasePath pathTo)
        {
            Debug.Assert(pathTo.IsRelativePath);
            // %HYYKA%

            /*if (pathTo.PathMode == PathMode.Absolute) {
             * throw new ArgumentException(@"Cannot call GetAbsolutePath() on a path already absolute.
             * PathFrom = """ + pathFrom.Path + @"""
             * PathTo   = """ + pathTo.Path + @"""");
             * }*/
            // Only work with Directory
            if (pathTo.IsFilePath)
            {
                pathTo = pathTo.ParentDirectoryPath;
            }
            return(InternalStringHelper.GetAbsolutePath(pathFrom.Path, pathTo.Path));
        }
示例#9
0
        //
        //  Relative/absolute
        //
        protected static string GetPathRelative(DirectoryPathAbsolute pathFrom, BasePath pathTo)
        {
            Debug.Assert(pathTo.IsAbsolutePath);
            // %HYYKA%

            /*if (pathTo.PathMode == PathMode.Relative) {
             * throw new ArgumentException(@"Cannot input a relative path to GetPathRelativeTo().
             * PathFrom = """ + pathFrom.Path + @"""
             * PathTo   = """ + pathTo.Path + @"""");
             * }*/
            if (string.Compare(pathFrom.DriveProtected, pathTo.DriveProtected, true) != 0)
            {
                throw new ArgumentException(@"Cannot compute relative path from 2 paths that are not on the same drive 
PathFrom = """ + pathFrom.Path + @"""
PathTo   = """ + pathTo.Path + @"""");
            }
            // Only work with Directory
            if (pathTo.IsFilePath)
            {
                pathTo = pathTo.ParentDirectoryPath;
            }
            return(InternalStringHelper.GetPathRelativeTo(pathFrom.Path, pathTo.Path));
        }
示例#10
0
        public static string AbsolutePath(string path)
        {
            if (string.IsNullOrEmpty(path) || Path.IsPathRooted(path))
            {
                return(path);
            }

            ///////// EDITED BY LMT 27/NOV/2008 for NHT-11
            string reason;

            if (!PathHelper.IsValidRelativePath(path, out reason))
            {
                if (!path.StartsWith(@".\"))
                {
                    path = @".\" + path;
                    if (!PathHelper.IsValidRelativePath(path, out reason))
                    {
                        throw new ArgumentException(string.Format("{0}. {1}", path, reason));
                    }
                }
                else
                {
                    throw new ArgumentException(string.Format("{0}. {1}", path, reason));
                }
            }
            FilePathRelative relativePath = new FilePathRelative(path);
            ///////// END EDITED BY LMT 27/NOV/2008 for NHT-11

            DirectoryPathAbsolute absolutePath = new DirectoryPathAbsolute(AppDomain.CurrentDomain.BaseDirectory);

            if (!relativePath.CanGetAbsolutePathFrom(absolutePath))
            {
                throw new ArgumentException(string.Format("Invalid path, " + path));
            }
            return(relativePath.GetAbsolutePathFrom(absolutePath).Path);
        }
示例#11
0
 //
 //  Relative/absolute
 //
 protected static string GetPathRelative(DirectoryPathAbsolute pathFrom, BasePath pathTo)
 {
     Debug.Assert(pathTo.IsAbsolutePath);
      // %HYYKA%
      /*if (pathTo.PathMode == PathMode.Relative) {
     throw new ArgumentException(@"Cannot input a relative path to GetPathRelativeTo().
     PathFrom = """ + pathFrom.Path + @"""
     PathTo   = """ + pathTo.Path + @"""");
      }*/
      if (string.Compare(pathFrom.DriveProtected, pathTo.DriveProtected, true) != 0) {
     throw new ArgumentException(@"Cannot compute relative path from 2 paths that are not on the same drive
     PathFrom = """ + pathFrom.Path + @"""
     PathTo   = """ + pathTo.Path + @"""");
      }
      // Only work with Directory
      if (pathTo.IsFilePath) { pathTo = pathTo.ParentDirectoryPath; }
      return InternalStringHelper.GetPathRelativeTo(pathFrom.Path, pathTo.Path);
 }
示例#12
0
 public bool IsChildDirectoryOf(DirectoryPathAbsolute parentDir)
 {
     if (parentDir == null) { throw new ArgumentNullException("parentDir"); }
      if (parentDir.IsEmpty) { throw new ArgumentException("Empty parentDir not accepted", "parentDir"); }
      string parentPathUpperCase = parentDir.Path.ToUpper();
      string thisPathUpperCase = this.Path.ToUpper();
      return thisPathUpperCase.Contains(parentPathUpperCase);
 }
示例#13
0
        public static string RelativePath(string absolutePath)
        {
            Check.Require(!string.IsNullOrEmpty(absolutePath), "absolutePath must not be null or empty.");

               if (!Path.IsPathRooted(absolutePath))
               throw new ArgumentException(absolutePath + " must be an absolute path.");

               DirectoryPathAbsolute baseDirectory =
               new DirectoryPathAbsolute(AppDomain.CurrentDomain.BaseDirectory);

               DirectoryPathAbsolute directory =
               new DirectoryPathAbsolute(absolutePath);

               return directory.GetPathRelativeFrom(baseDirectory).Path;
        }
示例#14
0
 //
 //  Absolute/Relative path conversion
 //
 public DirectoryPathRelative GetPathRelativeFrom(DirectoryPathAbsolute path)
 {
     if (path == null) {
     throw new ArgumentNullException();
      }
      if (PathHelper.IsEmpty(this) || PathHelper.IsEmpty(path)) {
     throw new ArgumentException("Cannot compute a relative path from an empty path.");
      }
      return new DirectoryPathRelative(BasePath.GetPathRelative(path, this));
 }
示例#15
0
 public bool CanGetPathRelativeFrom(DirectoryPathAbsolute path)
 {
     try {
     this.GetPathRelativeFrom(path);
     return true;
      } catch { }
      return false;
 }
示例#16
0
 //
 //  Absolute/Relative path conversion
 //
 public FilePathRelative GetPathRelativeFrom(DirectoryPathAbsolute path)
 {
     if (path == null) {
     throw new ArgumentNullException();
      }
      if (PathHelper.IsEmpty(this) || PathHelper.IsEmpty(path)) {
     throw new ArgumentException("Cannot compute a relative path from an empty path.");
      }
      string pathRelative = BasePath.GetPathRelative(path, this);
      return new FilePathRelative(pathRelative + System.IO.Path.DirectorySeparatorChar + this.FileName);
 }
示例#17
0
        //----------------------------------------------
        //
        //   Try Rebase path
        //
        //----------------------------------------------
        // originalPath "A:\X1\X2\X3"  validPath "B:\Y1\X1"  result "B:\Y1\X1\X2\X3"   deeperCommonDirName ="X1"
        // originalPath "A:\X1\X2\X3"  validPath "B:\Y1\Y2"  result null               deeperCommonDirName =null
        // originalPath "A:\X1\X2\X3"  validPath "B:\X1\X2"  result "B:\X1\X2\X3"      deeperCommonDirName ="X2
        // originalPath "A:\X1\X2\X3"  validPath "B:\X2\X3"  result "B:\X2\X3"         deeperCommonDirName ="X3"
        // originalPath "A:\X1\X2\X3"  validPath "B:\X2"     result "B:\X2\X3"         deeperCommonDirName ="X2"
        // originalPath "A:\X1\X2\X3"  validPath "B:\X3\X2"  result "B:\X3\X2\X3"      deeperCommonDirName ="X2"
        // originalPath "A:\X1\X2\X3"  validPath "B:\X3\Y1"  result "B:\X3"            deeperCommonDirName ="X3"
        // originalPath "A:\X1\X2\X3"  validPath "A:\"       result null               deeperCommonDirName =null
        // originalPath "A:\X1\X2\X3"  validPath "A:\Y1"     result null               deeperCommonDirName =null
        // Algo:
        // 1) Find all common dir name between originalPath and validPath
        // 2) If no common dir name then return null, can't guess the path
        // 3) Get the deeper common dir name (deeper in validPath)
        // 4) Return Path(deeperCommonDirName)+ Pathes in originalPath after deeperCommonDirName
        public static bool TryRebasePath(
            DirectoryPathAbsolute originalPath, 
            DirectoryPathAbsolute validPath, 
            out DirectoryPathAbsolute rebasedPath)
        {
            rebasedPath = DirectoryPathAbsolute.Empty;
             if (PathHelper.IsNullOrEmpty(originalPath)) {
            return false;
             }
             if (PathHelper.IsNullOrEmpty(validPath)) {
            return false;
             }
             List<string> originalPathDirs = new List<string>(originalPath.Path.Split(Path.DirectorySeparatorChar));
             List<string> validPathDirs = new List<string>(validPath.Path.Split(Path.DirectorySeparatorChar));

             int indexInValidPathOfDeeperCommonDirName = -1;
             int indexInOriginalPathOfDeeperCommonDirName = -1;

             // We begin at 1 both loop to avoid Driver in pathes
             for (int validPathIndex = 1; validPathIndex < validPathDirs.Count; validPathIndex++) {
            for (int originalPathIndex = 1; originalPathIndex < originalPathDirs.Count; originalPathIndex++) {
               if (validPathDirs[validPathIndex].Length > 0 && // Avoid comparison of empty string that can happen if DirectorySeparatorChar at the end of pathes
                     string.Compare(validPathDirs[validPathIndex], originalPathDirs[originalPathIndex], true) == 0) {
                  indexInValidPathOfDeeperCommonDirName = validPathIndex;
                  indexInOriginalPathOfDeeperCommonDirName = originalPathIndex;
                  break;
               }
            }
             }
             if (indexInValidPathOfDeeperCommonDirName == -1) {
            // No common dir name, return null
            return false;
             }

             // Concate Path(deeperCommonDirName)+ Pathes in originalPath after deeperCommonDirName
             Debug.Assert(indexInOriginalPathOfDeeperCommonDirName >= 0);
             List<string> inferedDirNames = validPathDirs.GetRange(0, indexInValidPathOfDeeperCommonDirName);
             inferedDirNames.AddRange(
            originalPathDirs.GetRange(indexInOriginalPathOfDeeperCommonDirName, originalPathDirs.Count - indexInOriginalPathOfDeeperCommonDirName));

             string[] arrayInferedDirNames = new string[inferedDirNames.Count];
             inferedDirNames.CopyTo(arrayInferedDirNames);
             string inferedPath = string.Join(Path.DirectorySeparatorChar.ToString(), arrayInferedDirNames);
             rebasedPath = new DirectoryPathAbsolute(inferedPath);
             return true;
        }
示例#18
0
 protected static string GetAbsolutePathFrom(DirectoryPathAbsolute pathFrom, BasePath pathTo)
 {
     Debug.Assert(pathTo.IsRelativePath);
      // %HYYKA%
      /*if (pathTo.PathMode == PathMode.Absolute) {
     throw new ArgumentException(@"Cannot call GetAbsolutePath() on a path already absolute.
     PathFrom = """ + pathFrom.Path + @"""
     PathTo   = """ + pathTo.Path + @"""");
      }*/
      // Only work with Directory
      if (pathTo.IsFilePath) { pathTo = pathTo.ParentDirectoryPath; }
      return InternalStringHelper.GetAbsolutePath(pathFrom.Path, pathTo.Path);
 }