public void Test_InvalidInputAbsoluteURNPath() { string reason; Assert.IsFalse(PathHelper.IsValidAbsolutePath(@"http://www.NDepend.com", out reason)); DirectoryPath directoryPath = new DirectoryPathAbsolute(@"http://www.NDepend.com"); }
public void Test_GetRelativePathWithError6() { DirectoryPathAbsolute directoryPathTo = new DirectoryPathAbsolute(@"C:\Dir1"); Assert.IsFalse(directoryPathTo.CanGetPathRelativeFrom(null)); directoryPathTo.GetPathRelativeFrom(null); }
public void Test_IncoherentPathModeException1() { string reason; Assert.IsFalse(PathHelper.IsValidAbsolutePath(@".", out reason)); DirectoryPath directoryPath = new DirectoryPathAbsolute(@"."); }
public void Test_TryGetCommonRootDirectory() { DirectoryPathAbsolute commonRootDirectory = null; // Test when list is null or empty Assert.IsFalse(ListOfPathHelper.TryGetCommonRootDirectory(null, out commonRootDirectory)); List <DirectoryPathAbsolute> list = new List <DirectoryPathAbsolute>(); Assert.IsFalse(ListOfPathHelper.TryGetCommonRootDirectory(list, out commonRootDirectory)); // Test when only one dir list.Add(new DirectoryPathAbsolute(@"C:\File")); Assert.IsTrue(ListOfPathHelper.TryGetCommonRootDirectory(list, out commonRootDirectory)); Assert.IsTrue(commonRootDirectory.Path == @"C:\File"); // Test when all dir are the same list.Add(new DirectoryPathAbsolute(@"C:\File")); list.Add(new DirectoryPathAbsolute(@"C:\File")); Assert.IsTrue(ListOfPathHelper.TryGetCommonRootDirectory(list, out commonRootDirectory)); Assert.IsTrue(commonRootDirectory.Path == @"C:\File"); // Test when a dir has a wrong drive list.Add(new DirectoryPathAbsolute(@"D:\File")); Assert.IsFalse(ListOfPathHelper.TryGetCommonRootDirectory(list, out commonRootDirectory)); // Test when the list contains a null or empty dir list.Clear(); list.Add(new DirectoryPathAbsolute(@"C:\File")); list.Add(null); Assert.IsFalse(ListOfPathHelper.TryGetCommonRootDirectory(list, out commonRootDirectory)); list.Clear(); list.Add(new DirectoryPathAbsolute(@"C:\File")); list.Add(DirectoryPathAbsolute.Empty); Assert.IsFalse(ListOfPathHelper.TryGetCommonRootDirectory(list, out commonRootDirectory)); // Test when the common root dir is in the list list.Clear(); list.Add(new DirectoryPathAbsolute(@"C:\File\Debug")); list.Add(new DirectoryPathAbsolute(@"C:\File\Debug\Dir1\Dir2")); list.Add(new DirectoryPathAbsolute(@"C:\File\Debug\Dir1\Dir2\Dir3")); Assert.IsTrue(ListOfPathHelper.TryGetCommonRootDirectory(list, out commonRootDirectory)); Assert.IsTrue(commonRootDirectory.Path == @"C:\File\Debug"); list.Add(new DirectoryPathAbsolute(@"C:\File")); Assert.IsTrue(ListOfPathHelper.TryGetCommonRootDirectory(list, out commonRootDirectory)); Assert.IsTrue(commonRootDirectory.Path == @"C:\File"); list.Add(new DirectoryPathAbsolute(@"C:")); Assert.IsTrue(ListOfPathHelper.TryGetCommonRootDirectory(list, out commonRootDirectory)); Assert.IsTrue(commonRootDirectory.Path == @"C:"); // Test when the common root dir is not in the list list.Clear(); list.Add(new DirectoryPathAbsolute(@"C:\File\Debug\Dir4")); list.Add(new DirectoryPathAbsolute(@"C:\File\Debug\Dir1\Dir2\Dir3")); Assert.IsTrue(ListOfPathHelper.TryGetCommonRootDirectory(list, out commonRootDirectory)); Assert.IsTrue(commonRootDirectory.Path == @"C:\File\Debug"); }
public void Test_GetAbsolutePath() { DirectoryPathRelative directoryPathTo; DirectoryPathAbsolute directoryPathFrom; directoryPathTo = new DirectoryPathRelative(@".."); directoryPathFrom = new DirectoryPathAbsolute(@"C:\Dir1"); Assert.IsTrue(directoryPathTo.GetAbsolutePathFrom(directoryPathFrom).Path == @"C:"); Assert.IsTrue(directoryPathTo.CanGetAbsolutePathFrom(directoryPathFrom)); directoryPathTo = new DirectoryPathRelative(@"."); directoryPathFrom = new DirectoryPathAbsolute(@"C:\Dir1"); Assert.IsTrue(directoryPathTo.GetAbsolutePathFrom(directoryPathFrom).Path == @"C:\Dir1"); Assert.IsTrue(directoryPathTo.CanGetAbsolutePathFrom(directoryPathFrom)); directoryPathTo = new DirectoryPathRelative(@"..\Dir2"); directoryPathFrom = new DirectoryPathAbsolute(@"C:\Dir1"); Assert.IsTrue(directoryPathTo.GetAbsolutePathFrom(directoryPathFrom).Path == @"C:\Dir2"); Assert.IsTrue(directoryPathTo.CanGetAbsolutePathFrom(directoryPathFrom)); directoryPathTo = new DirectoryPathRelative(@"..\..\Dir4\Dir5"); directoryPathFrom = new DirectoryPathAbsolute(@"C:\Dir1\Dir2\Dir3"); Assert.IsTrue(directoryPathTo.GetAbsolutePathFrom(directoryPathFrom).Path == @"C:\Dir1\Dir4\Dir5"); Assert.IsTrue(directoryPathTo.CanGetAbsolutePathFrom(directoryPathFrom)); directoryPathTo = new DirectoryPathRelative(@".\..\Dir4\Dir5"); directoryPathFrom = new DirectoryPathAbsolute(@"C:\Dir1\Dir2\Dir3"); Assert.IsTrue(directoryPathTo.GetAbsolutePathFrom(directoryPathFrom).Path == @"C:\Dir1\Dir2\Dir4\Dir5"); Assert.IsTrue(directoryPathTo.CanGetAbsolutePathFrom(directoryPathFrom)); }
public void Test_DirDontExist() { DirForTest.Delete(); string dirForTestPath = DirForTest.Dir; DirectoryPathAbsolute directoryPathAbsolute = new DirectoryPathAbsolute(dirForTestPath); DirectoryInfo directoryInfo = directoryPathAbsolute.DirectoryInfo; }
public void Test_DirDontExist() { DirForTest.Delete(); string dirForTestPath = DirForTest.Dir; DirectoryPathAbsolute directoryPathAbsolute = new DirectoryPathAbsolute(dirForTestPath); DirectoryInfo directoryInfo = directoryPathAbsolute.DirectoryInfo; }
public string GetRelativePath(string absoluteFilePath, string absoluteDirectoryPath) { FilePathAbsolute filePathAbsolute1 = new FilePathAbsolute(absoluteFilePath); DirectoryPathAbsolute directoryPathAbsolute1 = new DirectoryPathAbsolute(absoluteDirectoryPath); FilePathRelative filePathRelative1 = filePathAbsolute1.GetPathRelativeFrom(directoryPathAbsolute1); return filePathRelative1.Path; }
public bool CanGetAbsolutePathFrom(DirectoryPathAbsolute path) { try { this.GetAbsolutePathFrom(path); return true; } catch { } return false; }
public static void GetListOfUniqueDirsAndUniqueFileNames( List <FilePathAbsolute> listOfFilePath, out List <DirectoryPathAbsolute> listOfUniqueDirs, out List <string> listOfUniqueFileNames) { listOfUniqueDirs = new List <DirectoryPathAbsolute>(); listOfUniqueFileNames = new List <string>(); if (listOfFilePath == null) { return; } foreach (FilePathAbsolute filePath in listOfFilePath) { if (PathHelper.IsNullOrEmpty(filePath)) { continue; } DirectoryPathAbsolute dir = filePath.ParentDirectoryPath; if (!ListOfPathHelper.Contains(listOfUniqueDirs, dir)) { listOfUniqueDirs.Add(dir); } string fileName = filePath.FileName; Debug.Assert(fileName != null && fileName.Length > 0); if (!ListOfStringHelperContainsIgnoreCase(listOfUniqueFileNames, fileName)) { listOfUniqueFileNames.Add(fileName); } } // end foreach }
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); }
public void Test_GetAbsolutePath() { FilePathRelative filePathTo; DirectoryPathAbsolute directoryPathFrom; filePathTo = new FilePathRelative(@"..\File.txt"); directoryPathFrom = new DirectoryPathAbsolute(@"C:\Dir1"); Assert.IsTrue(filePathTo.GetAbsolutePathFrom(directoryPathFrom).Path == @"C:\File.txt"); Assert.IsTrue(filePathTo.CanGetAbsolutePathFrom(directoryPathFrom)); filePathTo = new FilePathRelative(@".\File.txt"); directoryPathFrom = new DirectoryPathAbsolute(@"C:\Dir1"); Assert.IsTrue(filePathTo.GetAbsolutePathFrom(directoryPathFrom).Path == @"C:\Dir1\File.txt"); Assert.IsTrue(filePathTo.CanGetAbsolutePathFrom(directoryPathFrom)); filePathTo = new FilePathRelative(@"..\Dir2\File.txt"); directoryPathFrom = new DirectoryPathAbsolute(@"C:\Dir1"); Assert.IsTrue(filePathTo.GetAbsolutePathFrom(directoryPathFrom).Path == @"C:\Dir2\File.txt"); Assert.IsTrue(filePathTo.CanGetAbsolutePathFrom(directoryPathFrom)); filePathTo = new FilePathRelative(@"..\..\Dir4\Dir5\File.txt"); directoryPathFrom = new DirectoryPathAbsolute(@"C:\Dir1\Dir2\Dir3"); Assert.IsTrue(filePathTo.GetAbsolutePathFrom(directoryPathFrom).Path == @"C:\Dir1\Dir4\Dir5\File.txt"); Assert.IsTrue(filePathTo.CanGetAbsolutePathFrom(directoryPathFrom)); filePathTo = new FilePathRelative(@".\..\Dir4\Dir5\File.txt"); directoryPathFrom = new DirectoryPathAbsolute(@"C:\Dir1\Dir2\Dir3"); Assert.IsTrue(filePathTo.GetAbsolutePathFrom(directoryPathFrom).Path == @"C:\Dir1\Dir2\Dir4\Dir5\File.txt"); Assert.IsTrue(filePathTo.CanGetAbsolutePathFrom(directoryPathFrom)); }
public void Test_InvalidInputAbsolutePath3() { string reason; Assert.IsFalse(PathHelper.IsValidAbsolutePath(@"1:\Dir1", out reason)); DirectoryPath directoryPath = new DirectoryPathAbsolute(@"1:\Dir1"); }
public void Test_GetAbsolutePathPathWithError3() { DirectoryPathRelative directoryPathTo = new DirectoryPathRelative(@"..\..\Dir1"); DirectoryPathAbsolute directoryPathFrom = new DirectoryPathAbsolute(@"C:\Dir1"); Assert.IsFalse(directoryPathTo.CanGetAbsolutePathFrom(directoryPathFrom)); directoryPathTo.GetAbsolutePathFrom(directoryPathFrom); }
public void Test_GetAbsolutePathPathWithError5() { FilePathRelative directoryPathTo = new FilePathRelative(@"..\..\Dir1\File.txt"); DirectoryPathAbsolute directoryPathFrom = DirectoryPathAbsolute.Empty; Assert.IsFalse(directoryPathTo.CanGetAbsolutePathFrom(directoryPathFrom)); directoryPathTo.GetAbsolutePathFrom(directoryPathFrom); }
public void Test_GetAbsolutePathPathWithError4() { FilePathRelative directoryPathTo = FilePathRelative.Empty; DirectoryPathAbsolute directoryPathFrom = new DirectoryPathAbsolute(@"C:\Dir1"); Assert.IsFalse(directoryPathTo.CanGetAbsolutePathFrom(directoryPathFrom)); directoryPathTo.GetAbsolutePathFrom(directoryPathFrom); }
public void Test_InvalidInputPathNull() { string reason; Assert.IsFalse(PathHelper.IsValidAbsolutePath(null, out reason)); Assert.IsFalse(PathHelper.IsValidRelativePath(null, out reason)); DirectoryPath directoryPath = new DirectoryPathAbsolute(null); }
public void Test_GetRelativePathWithError4() { DirectoryPathAbsolute directoryPathTo = DirectoryPathAbsolute.Empty; DirectoryPathAbsolute directoryPathFrom = new DirectoryPathAbsolute(@"D:\Dir1"); Assert.IsFalse(directoryPathTo.CanGetPathRelativeFrom(directoryPathFrom)); directoryPathTo.GetPathRelativeFrom(directoryPathFrom); }
public void Test_GetRelativePathWithError5() { FilePathAbsolute filePathTo = new FilePathAbsolute(@"C:\Dir1\File.txt"); DirectoryPathAbsolute directoryPathFrom = DirectoryPathAbsolute.Empty; Assert.IsFalse(filePathTo.CanGetPathRelativeFrom(directoryPathFrom)); filePathTo.GetPathRelativeFrom(directoryPathFrom); }
public bool CanGetAbsolutePathFrom(DirectoryPathAbsolute path) { try { this.GetAbsolutePathFrom(path); return(true); } catch { } return(false); }
public TempDirectoryPathAbsolute() { var guid = Guid.NewGuid(); _path = _tempPathRoot.GetChildDirectoryWithName (guid.ToString()); Directory.CreateDirectory (_path.Path); }
public void Test_InvalidInputPathEmpty() { string reason; Assert.IsFalse(PathHelper.IsValidAbsolutePath(string.Empty, out reason)); Assert.IsFalse(PathHelper.IsValidRelativePath(string.Empty, out reason)); DirectoryPath directoryPath = new DirectoryPathAbsolute(string.Empty); }
// // 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)); }
public void Test_EmptyDirectoryPathAbsolute() { DirectoryPathAbsolute directoryPath = DirectoryPathAbsolute.Empty; Assert.IsTrue(directoryPath.IsEmpty); Assert.IsTrue(directoryPath.IsAbsolutePath); Assert.IsTrue(directoryPath.IsDirectoryPath); Assert.IsFalse(directoryPath.IsFilePath); }
public void Test_GetHashCodeThroughDictionary() { Dictionary <BasePath, string> dico = new Dictionary <BasePath, string>(); DirectoryPathAbsolute directoryPath = new DirectoryPathAbsolute(@"C:\Dir1"); FilePathAbsolute filePath = new FilePathAbsolute(@"c:\dir1\Dir2\file.txt"); dico.Add(directoryPath, directoryPath.Path); dico.Add(filePath, filePath.Path); Assert.IsTrue(dico[filePath] == @"c:\dir1\Dir2\file.txt"); }
public void Test_GetHashCodeThroughDictionary() { Dictionary<BasePath, string> dico = new Dictionary<BasePath, string>(); DirectoryPathAbsolute directoryPath = new DirectoryPathAbsolute(@"C:\Dir1"); FilePathAbsolute filePath = new FilePathAbsolute(@"c:\dir1\Dir2\file.txt"); dico.Add(directoryPath, directoryPath.Path); dico.Add(filePath, filePath.Path); Assert.IsTrue(dico[filePath] == @"c:\dir1\Dir2\file.txt"); }
// // 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); }
public void Test_Drive() { DirectoryPathAbsolute directoryPathAbsolute = new DirectoryPathAbsolute(@"C:\Dir1"); Assert.IsTrue(directoryPathAbsolute.Drive == "C"); directoryPathAbsolute = new DirectoryPathAbsolute(@"c:\Dir1"); Assert.IsTrue(directoryPathAbsolute.Drive == "c"); FilePathAbsolute filePathAbsolute = new FilePathAbsolute(@"C:\Dir1\File.txt"); Assert.IsTrue(filePathAbsolute.Drive == "C"); filePathAbsolute = new FilePathAbsolute(@"c:\Dir1\File.txt"); Assert.IsTrue(filePathAbsolute.Drive == "c"); }
/// <summary> /// This method assumes that relative paths are relative to /// the environment's current directory. /// </summary> /// <param name="path"></param> /// <returns></returns> public static DirectoryPathAbsolute MakeAbsoluteAssumeCurrentDir(this DirectoryPath path) { DirectoryPathAbsolute absolute; var currentDir = new DirectoryPathAbsolute (Environment.CurrentDirectory); if (path.IsAbsolutePath) absolute = (DirectoryPathAbsolute) path; else absolute = ((DirectoryPathRelative) path).GetAbsolutePathFrom (currentDir); return absolute; }
//---------------------------------------------- // // 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); }
public void Test_GetChildWithName() { DirectoryPathAbsolute directoryPathAbsolute = new DirectoryPathAbsolute(@"C:\Dir1\Dir2"); Assert.IsTrue(directoryPathAbsolute.GetChildFileWithName("File.txt").Path == @"C:\Dir1\Dir2\File.txt"); Assert.IsTrue(directoryPathAbsolute.GetChildDirectoryWithName("Dir3").Path == @"C:\Dir1\Dir2\Dir3"); DirectoryPathRelative directoryPathRelative = new DirectoryPathRelative(@"..\..\Dir1\Dir2"); Assert.IsTrue(directoryPathRelative.GetChildFileWithName("File.txt").Path == @"..\..\Dir1\Dir2\File.txt"); Assert.IsTrue(directoryPathRelative.GetChildDirectoryWithName("Dir3").Path == @"..\..\Dir1\Dir2\Dir3"); }
// // 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))); }
public void Test_DirInfo_FileInfo_ChildrenOfDir() { DirForTest.EnsureDirForTestExistAndEmpty(); string dirForTestPath = DirForTest.Dir; DirectoryPathAbsolute directoryPathAbsolute = new DirectoryPathAbsolute(dirForTestPath); DirectoryInfo directoryInfo = directoryPathAbsolute.DirectoryInfo; Assert.IsTrue(directoryInfo != null); DirForTest.CopyExecutingAssemblyFileInDirForTest(); string dirForTestWithExecutingAssemblyFilePath = DirForTest.ExecutingAssemblyFilePathInDirForTest; FilePathAbsolute filePathAbsolute = new FilePathAbsolute(dirForTestWithExecutingAssemblyFilePath); FileInfo fileInfo = filePathAbsolute.FileInfo; Assert.IsTrue(fileInfo != null); }
// // 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)); }
public void Test_Drive() { DirectoryPathAbsolute directoryPathAbsolute = new DirectoryPathAbsolute(@"C:\Dir1"); Assert.IsTrue(directoryPathAbsolute.Drive == "C"); directoryPathAbsolute = new DirectoryPathAbsolute(@"c:\Dir1"); Assert.IsTrue(directoryPathAbsolute.Drive == "c"); FilePathAbsolute filePathAbsolute = new FilePathAbsolute(@"C:\Dir1\File.txt"); Assert.IsTrue(filePathAbsolute.Drive == "C"); filePathAbsolute = new FilePathAbsolute(@"c:\Dir1\File.txt"); Assert.IsTrue(filePathAbsolute.Drive == "c"); }
public void Test_ChildrenOfDir() { DirForTest.EnsureDirForTestExistAndEmpty(); string dirForTestPath = DirForTest.Dir; DirectoryPathAbsolute directoryPathAbsolute = new DirectoryPathAbsolute(dirForTestPath); Assert.IsTrue(directoryPathAbsolute.ChildrenDirectoriesPath.Count == 0); Assert.IsTrue(directoryPathAbsolute.ChildrenFilesPath.Count == 0); Directory.CreateDirectory(dirForTestPath + System.IO.Path.DirectorySeparatorChar + "Dir1"); Directory.CreateDirectory(dirForTestPath + System.IO.Path.DirectorySeparatorChar + "Dir2"); Assert.IsTrue(directoryPathAbsolute.ChildrenDirectoriesPath.Count == 2); DirForTest.CopyExecutingAssemblyFileInDirForTest(); Assert.IsTrue(directoryPathAbsolute.ChildrenFilesPath.Count == 1); }
public void Test_PathModeOk() { DirectoryPath path = new DirectoryPathRelative(@"."); Assert.IsTrue(path.IsRelativePath); path = new DirectoryPathAbsolute(@"C:\"); Assert.IsTrue(path.IsAbsolutePath); path = new DirectoryPathRelative(@".\dir...1"); Assert.IsTrue(path.IsRelativePath); path = new DirectoryPathAbsolute(@"C:\dir...1"); Assert.IsTrue(path.IsAbsolutePath); }
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)); }
public void Test_DirInfo_FileInfo_ChildrenOfDir() { DirForTest.EnsureDirForTestExistAndEmpty(); string dirForTestPath = DirForTest.Dir; DirectoryPathAbsolute directoryPathAbsolute = new DirectoryPathAbsolute(dirForTestPath); DirectoryInfo directoryInfo = directoryPathAbsolute.DirectoryInfo; Assert.IsTrue(directoryInfo != null); DirForTest.CopyExecutingAssemblyFileInDirForTest(); string dirForTestWithExecutingAssemblyFilePath = DirForTest.ExecutingAssemblyFilePathInDirForTest; FilePathAbsolute filePathAbsolute = new FilePathAbsolute(dirForTestWithExecutingAssemblyFilePath); FileInfo fileInfo = filePathAbsolute.FileInfo; Assert.IsTrue(fileInfo != null); }
public void Test_ChildrenOfDir() { DirForTest.EnsureDirForTestExistAndEmpty(); string dirForTestPath = DirForTest.Dir; DirectoryPathAbsolute directoryPathAbsolute = new DirectoryPathAbsolute(dirForTestPath); Assert.IsTrue(directoryPathAbsolute.ChildrenDirectoriesPath.Count == 0); Assert.IsTrue(directoryPathAbsolute.ChildrenFilesPath.Count == 0); Directory.CreateDirectory(dirForTestPath + System.IO.Path.DirectorySeparatorChar + "Dir1"); Directory.CreateDirectory(dirForTestPath + System.IO.Path.DirectorySeparatorChar + "Dir2"); Assert.IsTrue(directoryPathAbsolute.ChildrenDirectoriesPath.Count == 2); DirForTest.CopyExecutingAssemblyFileInDirForTest(); Assert.IsTrue(directoryPathAbsolute.ChildrenFilesPath.Count == 1); }
public void Test_Exist() { DirForTest.Delete(); string dirForTestPath = DirForTest.Dir; DirectoryPathAbsolute directoryPathAbsolute = new DirectoryPathAbsolute(dirForTestPath); Assert.IsFalse(directoryPathAbsolute.Exists); DirForTest.EnsureDirForTestExistAndEmpty(); Assert.IsTrue(directoryPathAbsolute.Exists); string dirForTestWithExecutingAssemblyFilePath = DirForTest.ExecutingAssemblyFilePathInDirForTest; FilePathAbsolute filePathAbsolute = new FilePathAbsolute(dirForTestWithExecutingAssemblyFilePath); Assert.IsFalse(filePathAbsolute.Exists); DirForTest.CopyExecutingAssemblyFileInDirForTest(); Assert.IsTrue(filePathAbsolute.Exists); }
protected static string GetAbsolutePathFrom(DirectoryPathAbsolute pathFrom, BasePath pathTo) { Debug.Assert(pathTo.IsRelativePath); /*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)); }
public Disc(string path, string type) { var dir = new DirectoryPathAbsolute(path); this.Path = path; this.Type = type; this.Name = dir.DirectoryName; var exts = new List<string>(); if (dir.ChildrenFilesPath.Any(x => x.FileExtension == ".xml")) exts.Add("xml"); else if (dir.ChildrenFilesPath.Any(x => x.FileExtension == ".chapters")) exts.Add("chapter"); else if (dir.ChildrenFilesPath.Any(x => x.FileExtension == ".txt")) exts.Add("txt"); this.FoundExtractions = exts.ToArray(); }
public void TestDirectoryName() { string directoryName = new DirectoryPathRelative(@".\").DirectoryName; Assert.IsTrue(directoryName == string.Empty); directoryName = new DirectoryPathAbsolute(@"C:\").DirectoryName; Assert.IsTrue(directoryName == string.Empty); directoryName = new DirectoryPathRelative(@".\\dir1\\/dir2").DirectoryName; Assert.IsTrue(directoryName == @"dir2"); directoryName = new DirectoryPathAbsolute(@"C:\\\\dir1").DirectoryName; Assert.IsTrue(directoryName == @"dir1"); directoryName = new DirectoryPathAbsolute(@"C:\dir1\\//dir2\").DirectoryName; Assert.IsTrue(directoryName == @"dir2"); }
public void TestDirectoryName() { string directoryName = new DirectoryPathRelative(@".\").DirectoryName; Assert.IsTrue(directoryName == string.Empty); directoryName = new DirectoryPathAbsolute(@"C:\").DirectoryName; Assert.IsTrue(directoryName == string.Empty); directoryName = new DirectoryPathRelative(@".\\dir1\\/dir2").DirectoryName; Assert.IsTrue(directoryName == @"dir2"); directoryName = new DirectoryPathAbsolute(@"C:\\\\dir1").DirectoryName; Assert.IsTrue(directoryName == @"dir1"); directoryName = new DirectoryPathAbsolute(@"C:\dir1\\//dir2\").DirectoryName; Assert.IsTrue(directoryName == @"dir2"); }
public static string GetClosestExistingDirectory(string dir) { if (string.IsNullOrEmpty(dir)) return null; try { DirectoryPathAbsolute last = new DirectoryPathAbsolute(dir); if (last.Exists) return last.Path; else if (last.ParentDirectoryPath.Exists) return last.ParentDirectoryPath.Path; else if (last.ParentDirectoryPath.ParentDirectoryPath.Exists) return last.ParentDirectoryPath.ParentDirectoryPath.Path; } catch (Exception ex) { Trace.Write(ex); } return null; }
private void QuickOpen(string dir) { this.txtFolder.Text = dir; this.flowDiscs.Controls.Clear(); if (string.IsNullOrEmpty(dir)) { this.flowDiscs.Controls.Add(new ErrorItem("Please choose a folder containing your disc backups.")); return; } try { var discs = new List<Disc>(); var dirs = new DirectoryPathAbsolute(dir).ChildrenDirectoriesPath; foreach (var d in dirs) { if (Directory.Exists(Path.Combine(d.Path, "VIDEO_TS"))) discs.Add(new Disc(d.Path, "DVD")); else if (Directory.Exists(Path.Combine(Path.Combine(d.Path, "BDMV"), "PLAYLIST"))) discs.Add(new Disc(d.Path, "Bluray")); else if (Directory.Exists(Path.Combine(d.Path, "ADV_OBJ"))) discs.Add(new Disc(d.Path, "HDDVD")); } foreach (var disc in discs) { var di = new DiscItem(disc); di.Opened += (s, e) => { this.DiscPath = ((Disc)((DiscItem)s).Tag).Path; this.DialogResult = System.Windows.Forms.DialogResult.OK; this.Close(); }; this.flowDiscs.Controls.Add(di); } if (discs.Count == 0) { this.flowDiscs.Controls.Add(new ErrorItem("No discs found. Please select your backup folder containing discs.")); } } catch (Exception ex) { this.flowDiscs.Controls.Add(new ErrorItem(ex.Message)); } }
public void Test_PathEquality() { Assert.IsFalse(DirectoryPathAbsolute.Empty.Equals(null)); Assert.IsFalse(DirectoryPathAbsolute.Empty == null); Assert.IsTrue(DirectoryPathAbsolute.Empty.Equals(DirectoryPathRelative.Empty)); Assert.IsTrue(DirectoryPathAbsolute.Empty == DirectoryPathRelative.Empty); Assert.IsFalse(DirectoryPathAbsolute.Empty.Equals(new DirectoryPathRelative(@"..\Dir1\Dir2"))); Assert.IsFalse(DirectoryPathAbsolute.Empty == new DirectoryPathRelative(@"..\Dir1\Dir2")); // // directoryPathRelative // DirectoryPathRelative directoryPathRelative1 = new DirectoryPathRelative(@"..\Dir1\Dir2"); DirectoryPathRelative directoryPathRelative2 = new DirectoryPathRelative(@"..\\dir1//DIR2/"); Assert.IsTrue(directoryPathRelative1.Equals(directoryPathRelative2)); Assert.IsTrue(directoryPathRelative1 == directoryPathRelative2); directoryPathRelative1 = new DirectoryPathRelative(@"..\Dir1\Dir2"); directoryPathRelative2 = new DirectoryPathRelative(@".\Dir1\Dir2"); Assert.IsFalse(directoryPathRelative1.Equals(directoryPathRelative2)); Assert.IsFalse(directoryPathRelative1 == directoryPathRelative2); directoryPathRelative1 = new DirectoryPathRelative(@"..\Dir1\Dir2"); directoryPathRelative2 = new DirectoryPathRelative(@"..\Dir1\Dir2\Dir3"); Assert.IsFalse(directoryPathRelative1.Equals(directoryPathRelative2)); Assert.IsTrue(directoryPathRelative1 != directoryPathRelative2); directoryPathRelative1 = new DirectoryPathRelative(@"..\Dir1\Dir2"); directoryPathRelative2 = new DirectoryPathRelative(@"..\Dir1\Dir"); Assert.IsFalse(directoryPathRelative1.Equals(directoryPathRelative2)); Assert.IsTrue(directoryPathRelative1 != directoryPathRelative2); // // directoryPathAbsolute // DirectoryPathAbsolute directoryPathAbsolute1 = new DirectoryPathAbsolute(@"C:\Dir1\Dir2"); DirectoryPathAbsolute directoryPathAbsolute2 = new DirectoryPathAbsolute(@"C:\\dir1//Dir2\\"); Assert.IsTrue(directoryPathAbsolute1.Equals(directoryPathAbsolute2)); Assert.IsFalse(directoryPathAbsolute1 != directoryPathAbsolute2); directoryPathAbsolute1 = new DirectoryPathAbsolute(@"C:\Dir1\Dir2"); directoryPathAbsolute2 = new DirectoryPathAbsolute(@"D:\Dir1\Dir2"); Assert.IsFalse(directoryPathAbsolute1.Equals(directoryPathAbsolute2)); Assert.IsFalse(directoryPathAbsolute1 == directoryPathAbsolute2); directoryPathAbsolute1 = new DirectoryPathAbsolute(@"C:\Dir1\Dir2"); directoryPathAbsolute2 = new DirectoryPathAbsolute(@"C:\Dir1\Dir2\Dir2"); Assert.IsFalse(directoryPathAbsolute1.Equals(directoryPathAbsolute2)); Assert.IsFalse(directoryPathAbsolute1 == directoryPathAbsolute2); directoryPathAbsolute1 = new DirectoryPathAbsolute(@"C:\Dir1\Dir2"); directoryPathAbsolute2 = new DirectoryPathAbsolute(@"C:\Dir1\Dir"); Assert.IsFalse(directoryPathAbsolute1.Equals(directoryPathAbsolute2)); Assert.IsFalse(directoryPathAbsolute1 == directoryPathAbsolute2); // // Mix between directoryPathAbsolute and directoryPathRelative // directoryPathRelative1 = new DirectoryPathRelative(@"..\Dir1\Dir2"); directoryPathAbsolute1 = new DirectoryPathAbsolute(@"C:\Dir1\Dir2"); Assert.IsFalse(directoryPathAbsolute1.Equals(directoryPathRelative1)); Assert.IsFalse(directoryPathAbsolute1 == directoryPathRelative1); }
static void Main(string[] args) { FilePathAbsolute filePathAbsolute1, filePathAbsolute2; FilePathRelative filePathRelative1; DirectoryPathAbsolute directoryPathAbsolute1; DirectoryPathRelative directoryPathRelative1; // Path normalization filePathAbsolute1 = new FilePathAbsolute(@"C:/Dir1\\File.txt"); Debug.Assert(filePathAbsolute1.Path == @"C:\Dir1\File.txt"); directoryPathAbsolute1 = new DirectoryPathAbsolute(@"C:/Dir1\\Dir2\"); Debug.Assert(directoryPathAbsolute1.Path == @"C:\Dir1\Dir2"); directoryPathAbsolute1 = new DirectoryPathAbsolute(@"C:\Dir1\..\Dir2\."); Debug.Assert(directoryPathAbsolute1.Path == @"C:\Dir2"); // Path comparison filePathAbsolute1 = new FilePathAbsolute(@"C:/Dir1\\File.txt"); filePathAbsolute2 = new FilePathAbsolute(@"C:\DIR1\FILE.TXT"); Debug.Assert(filePathAbsolute1.Equals(filePathAbsolute2)); Debug.Assert(filePathAbsolute1 == filePathAbsolute2); // Relative -> Absolute path conversion filePathRelative1 = new FilePathRelative(@"..\..\Dir1\File.txt"); directoryPathAbsolute1 = new DirectoryPathAbsolute(@"C:\Dir2\Dir3\Dir4"); filePathAbsolute1 = filePathRelative1.GetAbsolutePathFrom(directoryPathAbsolute1); Debug.Assert( filePathAbsolute1.Path == @"C:\Dir2\Dir1\File.txt"); // Absolute -> Relative path conversion filePathAbsolute1 = new FilePathAbsolute(@"C:\Dir1\File.txt"); directoryPathAbsolute1 = new DirectoryPathAbsolute(@"C:\Dir2\Dir3\Dir4"); filePathRelative1 = filePathAbsolute1.GetPathRelativeFrom(directoryPathAbsolute1); Debug.Assert(filePathRelative1.Path == @"..\..\..\Dir1\File.txt"); // Path string validation string reason; Debug.Assert(PathHelper.IsValidAbsolutePath(@"C:\Dir2\Dir1", out reason)); Debug.Assert(!PathHelper.IsValidAbsolutePath(@"C:\..\Dir1", out reason)); Debug.Assert(!PathHelper.IsValidAbsolutePath(@".\Dir1", out reason)); Debug.Assert(!PathHelper.IsValidAbsolutePath(@"1:\Dir1", out reason)); Debug.Assert(PathHelper.IsValidRelativePath(@".\Dir1\Dir2", out reason)); Debug.Assert(PathHelper.IsValidRelativePath(@"..\Dir1\Dir2", out reason)); Debug.Assert(PathHelper.IsValidRelativePath(@".\Dir1\..\Dir2", out reason)); Debug.Assert(!PathHelper.IsValidRelativePath(@".\Dir1\..\..\Dir2", out reason)); Debug.Assert(!PathHelper.IsValidRelativePath(@"C:\Dir1\Dir2", out reason)); // File name & extension filePathAbsolute1 = new FilePathAbsolute(@"C:\Dir1\File.cs.Txt"); Debug.Assert(filePathAbsolute1.FileName == "File.cs.Txt"); Debug.Assert(filePathAbsolute1.FileNameWithoutExtension == "File.cs"); Debug.Assert(filePathAbsolute1.FileExtension == ".Txt"); Debug.Assert(filePathAbsolute1.HasExtension(".txt")); // Path browsing filePathAbsolute1 = new FilePathAbsolute(@"C:\Dir1\File.cs.Txt"); Debug.Assert(filePathAbsolute1.ParentDirectoryPath.Path == @"C:\Dir1"); Debug.Assert(filePathAbsolute1.GetBrotherFileWithName("File.xml").Path == @"C:\Dir1\File.xml"); Debug.Assert(filePathAbsolute1.ParentDirectoryPath.GetChildDirectoryWithName("Dir2").Path == @"C:\Dir1\Dir2"); Debug.Assert(filePathAbsolute1.ParentDirectoryPath.GetChildDirectoryWithName("..").Path == @"C:"); directoryPathRelative1 = new DirectoryPathRelative(@"..\Dir1\Dir2"); Debug.Assert(directoryPathRelative1.ParentDirectoryPath.Path == @"..\Dir1"); // Path rebasing directoryPathAbsolute1 = new DirectoryPathAbsolute(@"C:\Dir1\Dir2\Dir3"); DirectoryPathAbsolute directoryPathAbsolute2 = new DirectoryPathAbsolute(@"E:\Dir4\Dir1"); DirectoryPathAbsolute rebasedPath; PathHelper.TryRebasePath(directoryPathAbsolute1, directoryPathAbsolute2, out rebasedPath); Debug.Assert(rebasedPath.Path == @"E:\Dir4\Dir1\Dir2\Dir3"); // List of path ListOfPathsEquals \ Contains \ TryGetCommonRootDirectory List<DirectoryPathAbsolute> list1 = new List<DirectoryPathAbsolute>(); List<DirectoryPathAbsolute> list2 = new List<DirectoryPathAbsolute>(); list1.Add(new DirectoryPathAbsolute(@"C:\Dir1\Dir2")); list2.Add(new DirectoryPathAbsolute(@"c:\dir1\dir2")); list1.Add(new DirectoryPathAbsolute(@"C:\Dir1\Dir3\Dir4")); list2.Add(new DirectoryPathAbsolute(@"c:\dir1\dir3\dir4")); Debug.Assert(ListOfPathHelper.ListOfPathsEquals(list1, list2)); Debug.Assert(ListOfPathHelper.Contains(list1, new DirectoryPathAbsolute(@"C:\Dir1\dir2"))); ListOfPathHelper.TryGetCommonRootDirectory(list1, out directoryPathAbsolute1); Debug.Assert(directoryPathAbsolute1.Path == @"C:\Dir1"); // List of path GetListOfUniqueDirsAndUniqueFileNames List<FilePathAbsolute> list = new List<FilePathAbsolute>(); list.Add(new FilePathAbsolute(@"E:\Dir1\Dir2\File1.txt")); list.Add(new FilePathAbsolute(@"E:\dir1\dir2\File2.txt")); list.Add(new FilePathAbsolute(@"E:\Dir1\Dir2\Dir3\file2.txt")); List<DirectoryPathAbsolute> listOfUniqueDirs; List<string> listOfUniqueFileNames; ListOfPathHelper.GetListOfUniqueDirsAndUniqueFileNames(list, out listOfUniqueDirs, out listOfUniqueFileNames); Debug.Assert(listOfUniqueDirs.Count == 2); Debug.Assert(listOfUniqueDirs[0].Path == @"E:\Dir1\Dir2"); Debug.Assert(listOfUniqueDirs[1].Path == @"E:\Dir1\Dir2\Dir3"); Debug.Assert(listOfUniqueFileNames.Count == 2); Debug.Assert(listOfUniqueFileNames[0] == "File1.txt"); Debug.Assert(listOfUniqueFileNames[1] == "File2.txt"); // Interaction with System.IO API filePathAbsolute1 = new FilePathAbsolute( System.Reflection.Assembly.GetExecutingAssembly().Location); Debug.Assert(filePathAbsolute1.Exists); System.IO.FileInfo fileInfo = filePathAbsolute1.FileInfo; directoryPathAbsolute1 = filePathAbsolute1.ParentDirectoryPath as DirectoryPathAbsolute; Debug.Assert(directoryPathAbsolute1.Exists); System.IO.DirectoryInfo directoryInfo = directoryPathAbsolute1.DirectoryInfo; List<DirectoryPathAbsolute> listSubDir = directoryPathAbsolute1.ChildrenDirectoriesPath; List<FilePathAbsolute> listSubFile = directoryPathAbsolute1.ChildrenFilesPath; }
/// <summary> /// check if the search item path is a subdirectory of the rootDir /// </summary> /// <param name="rootDir"></param> /// <param name="searchItem"></param> /// <returns></returns> public static bool IsSubdirectory(string rootDir, string searchItem) { if (rootDir.StartsWith("\\")) //network disk? { return searchItem.StartsWith(rootDir, StringComparison.InvariantCultureIgnoreCase); } else { var root = new DirectoryPathAbsolute(rootDir); var search = new DirectoryPathAbsolute(searchItem); return search.IsChildDirectoryOf(root); } }
private void GetBat() { Microsoft.Win32.OpenFileDialog openFileDialog2 = new Microsoft.Win32.OpenFileDialog(); openFileDialog2.Filter = "Known file types (*.bat)|*.bat"; openFileDialog2.Title = "Select a batch file that will be run after completion of the model run"; if (openFileDialog2.ShowDialog().Value) { DirectoryPathAbsolute dp = new DirectoryPathAbsolute(Path.GetDirectoryName(models.First().DisplayName)); FilePathAbsolute fp = new FilePathAbsolute(openFileDialog2.FileName); postProcessBat = fp.GetPathRelativeFrom(dp); RaisePropertyChanged("PostProcessBat"); } }
public void Test_ParentDirectoryPath() { DirectoryPath path = new FilePathRelative(@".\Dir1").ParentDirectoryPath; Assert.IsTrue(path.Path == @"."); path = new DirectoryPathRelative(@".\Dir1\\Dir2").ParentDirectoryPath; Assert.IsTrue(path.Path == @".\Dir1"); path = new DirectoryPathAbsolute(@"C:\Dir1").ParentDirectoryPath; Assert.IsTrue(path.Path == @"C:"); path = new DirectoryPathRelative(@".\\Dir1").ParentDirectoryPath; Assert.IsTrue(path.Path == @"."); path = new DirectoryPathAbsolute(@"C:\\\\Dir1\\Dir2").ParentDirectoryPath; Assert.IsTrue(path.Path == @"C:\Dir1"); path = new DirectoryPathAbsolute(@"C:\dir1\\//dir2\").ParentDirectoryPath; Assert.IsTrue(path.Path == @"C:\dir1"); }
public void Test_NormalizePath() { DirectoryPath path = new DirectoryPathRelative(@".\"); Assert.IsTrue(path.Path == "."); path = new DirectoryPathRelative(@".\\\"); Assert.IsTrue(path.Path == "."); path = new DirectoryPathRelative(@".\\\..\\"); Assert.IsTrue(path.Path == @".\.."); path = new DirectoryPathRelative(@".\/dir1\//\dir2\/dir3///"); Assert.IsTrue(path.Path == @".\dir1\dir2\dir3"); path = new DirectoryPathAbsolute(@"C:/dir1/dir2"); Assert.IsTrue(path.Path == @"C:\dir1\dir2"); }
private void GetMshe() { Microsoft.Win32.OpenFileDialog openFileDialog2 = new Microsoft.Win32.OpenFileDialog(); openFileDialog2.Filter = "Known file types (*.she)|*.she"; openFileDialog2.Title = "Select the Mike She file corresponding to the first .pst-file"; if (openFileDialog2.ShowDialog().Value) { DirectoryPathAbsolute dp = new DirectoryPathAbsolute(Path.GetDirectoryName(models.First().DisplayName)); FilePathAbsolute fp = new FilePathAbsolute(openFileDialog2.FileName); mikeSheFileName = fp.GetPathRelativeFrom(dp); RaisePropertyChanged("MikeSheFileName"); } }
public void Test_PathModeOk() { DirectoryPath path = new DirectoryPathRelative(@"."); Assert.IsTrue(path.IsRelativePath); path = new DirectoryPathAbsolute(@"C:\"); Assert.IsTrue(path.IsAbsolutePath); path = new DirectoryPathRelative(@".\dir...1"); Assert.IsTrue(path.IsRelativePath); path = new DirectoryPathAbsolute(@"C:\dir...1"); Assert.IsTrue(path.IsAbsolutePath); }
/// <summary> /// Returns a relative path string from a full path. /// </summary> /// <returns></returns> public static string GetRelativePath(string rootDir, string filePath) { if (rootDir == null || filePath == null) return filePath; if (IsSubdirectory(rootDir, filePath)) { var filePathAbsolute = new FilePathAbsolute(filePath); var directoryPathAbsolute = new DirectoryPathAbsolute(rootDir); FilePathRelative filePathRelative = filePathAbsolute.GetPathRelativeFrom(directoryPathAbsolute); return filePathRelative.Path; } return filePath; }
//---------------------------------------------- // // 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; }
/// <summary> /// check if the search item path is a subdirectory of the rootDir /// </summary> /// <param name="rootDir"></param> /// <param name="searchItem"></param> /// <returns></returns> public static bool IsSubdirectory(string rootDir, string searchItem) { var root = new DirectoryPathAbsolute(rootDir); var search = new DirectoryPathAbsolute(searchItem); return search.IsChildDirectoryOf(root); }
private void Run() { DirectoryPathAbsolute dp = new DirectoryPathAbsolute(Path.GetDirectoryName(models.First().DisplayName)); ScenariosToRun = new ConcurrentStack<ScenarioRun>(Runs.Where(var=>var.RunThis)); foreach (var v in models) { v.ResultFileNames.Clear(); DirectoryPathAbsolute dp2 = new DirectoryPathAbsolute(Path.GetDirectoryName(v.DisplayName)); foreach (var file in FileNamesToCopy) { FilePathAbsolute fp = new FilePathAbsolute(file); var rp = fp.GetPathRelativeFrom(dp); v.ResultFileNames.Add(rp.GetAbsolutePathFrom(dp2).Path); } if (v is PestModel) { ((PestModel)v).MsheFileName = mikeSheFileName.GetAbsolutePathFrom(dp2).Path; ((PestModel)v).PostProcessBatFile = postProcessBat.GetAbsolutePathFrom(dp2).Path; } Thread.Sleep(TimeSpan.FromMinutes(1)); RunNext(v); } }
/// <summary> /// Returns a relative path string from a full path. /// </summary> /// <returns></returns> public static string GetRelativePath(string rootDir, string filePath) { if (rootDir == null || filePath == null) return filePath; if (IsSubdirectory(rootDir, filePath)) { if (rootDir.StartsWith("\\")) //network disk? { return "." + filePath.Substring(rootDir.Length); } else { var filePathAbsolute = new FilePathAbsolute(filePath); var directoryPathAbsolute = new DirectoryPathAbsolute(rootDir); FilePathRelative filePathRelative = filePathAbsolute.GetPathRelativeFrom(directoryPathAbsolute); return filePathRelative.Path; } } return filePath; }