private void UpdateSelectedFileDisplayName() { var selectedFile = SelectedFile; if (string.IsNullOrWhiteSpace(selectedFile) || !System.IO.Path.IsPathRooted(selectedFile)) { SelectedFileDisplayName = string.Empty; return; } var baseDirectory = BaseDirectory; if (string.IsNullOrWhiteSpace(baseDirectory) || !Directory.Exists(baseDirectory)) { SelectedFileDisplayName = selectedFile; return; } var selectedFileRoot = System.IO.Path.GetPathRoot(selectedFile); var baseDirectoryRoot = System.IO.Path.GetPathRoot(baseDirectory); if (!string.Equals(selectedFileRoot, baseDirectoryRoot, StringComparison.OrdinalIgnoreCase)) { SelectedFileDisplayName = selectedFile; return; } SelectedFileDisplayName = Path.GetRelativePath(selectedFile, baseDirectory); }
public void GetRelativePath_DifferentRoot() { // Declare variables string file = @"C:\Windows\notepad.exe"; string path = @"D:\Windows\"; // Call method string relative = Path.GetRelativePath(file, path); // Validate Assert.AreEqual(@"C:\Windows\notepad.exe".ToLower(), relative.ToLower()); }
public void GetRelativePath_HigherDirectory() { // Declare variables string file = @"C:\Windows\"; string path = @"C:\Windows\Level1\Level2"; // Call method string relative = Path.GetRelativePath(file, path); // Validate Assert.AreEqual(@"..\..".ToLower(), relative.ToLower()); }
public void GetRelativePath_SameDirectoryLevelWithDifferentName() { // Declare variables string file = @"C:\Windows\MyTest\MyFile.exe"; string path = @"C:\Windows\MyTes"; // Call method string relative = Path.GetRelativePath(file, path); // Validate Assert.AreEqual(@"..\MyTest\MyFile.exe".ToLower(), relative.ToLower()); }
public void GetRelativePath_DeeperDirectory() { // Declare variables string file = @"C:\Windows\notepad.exe"; string path = @"C:\Windows\Temp\"; // Call method string relative = Path.GetRelativePath(file, path); // Validate Assert.AreEqual(@"..\notepad.exe".ToLower(), relative.ToLower()); }
public void GetRelativePath_SingleDirectoryNotEndingWithSlash() { // Declare variables string file = @"C:\Windows\notepad.exe"; string path = @"C:\Windows"; // Call method string relative = Path.GetRelativePath(file, path); // Validate Assert.AreEqual(@"notepad.exe".ToLower(), relative.ToLower()); }
public void GetRelativePath_EmptyBasePath() { // Get current directory string currentWorkingDirectory = Environment.CurrentDirectory; Environment.CurrentDirectory = @"C:\Windows\System32"; // Declare variables string file = @"C:\Windows\MyTest\MyFile.exe"; // Call method string relative = Path.GetRelativePath(file); // Validate Assert.AreEqual(@"..\MyTest\MyFile.exe".ToLower(), relative.ToLower()); // Restore current working directory Environment.CurrentDirectory = currentWorkingDirectory; }
public void GetRelativePath_InvalidInput() { ExceptionTester.CallMethodAndExpectException <ArgumentException>(() => Path.GetRelativePath(null, @"C:\test\")); }