diff --git a/csharp-classes-struct-record/PathClassCsharp/PathClassCsharp/PathClassCsharp.csproj b/csharp-classes-struct-record/PathClassCsharp/PathClassCsharp/PathClassCsharp.csproj index ed3fc66ef2..213b53fe1c 100644 --- a/csharp-classes-struct-record/PathClassCsharp/PathClassCsharp/PathClassCsharp.csproj +++ b/csharp-classes-struct-record/PathClassCsharp/PathClassCsharp/PathClassCsharp.csproj @@ -1,7 +1,7 @@ Exe - net6.0 + net10.0 enable enable diff --git a/csharp-classes-struct-record/PathClassCsharp/PathClassCsharp/PathSamples.cs b/csharp-classes-struct-record/PathClassCsharp/PathClassCsharp/PathSamples.cs index 9b842f6c07..e267294691 100644 --- a/csharp-classes-struct-record/PathClassCsharp/PathClassCsharp/PathSamples.cs +++ b/csharp-classes-struct-record/PathClassCsharp/PathClassCsharp/PathSamples.cs @@ -20,7 +20,7 @@ public char AltDirectorySeparatorChar() public char PathSeparator() { - var result =Path.PathSeparator; + var result = Path.PathSeparator; Console.WriteLine($"Path.PathSeparator: '{result}'"); return result; @@ -46,13 +46,13 @@ public char[] GetInvalidPathChars() } Console.WriteLine(); - return (invalidChars); + return invalidChars; } - public bool EndsInDirectorySeparator(ReadOnlySpan filePath) + public bool EndsInDirectorySeparator(ReadOnlySpan filePath) { var result = Path.EndsInDirectorySeparator(filePath); - Console.WriteLine($"EndsInDirectorySeparator('{filePath.ToString()}') returns '{result.ToString()}'"); + Console.WriteLine($"EndsInDirectorySeparator('{filePath.ToString()}') returns '{result}'"); return result; } @@ -60,7 +60,7 @@ public bool EndsInDirectorySeparator(ReadOnlySpan filePath) public bool EndsInDirectorySeparator(string filePath) { var result = Path.EndsInDirectorySeparator(filePath); - Console.WriteLine($"EndsInDirectorySeparator('{filePath}') returns '{result.ToString()}'"); + Console.WriteLine($"EndsInDirectorySeparator('{filePath}') returns '{result}'"); return result; } @@ -75,10 +75,10 @@ public string GetExtension(string fileName) public string ChangeExtension(string path, string newExtension) { - var result= Path.ChangeExtension(path, newExtension); - Console.WriteLine($"ChangeExtension({path}, '.old') returns '{result}'"); + var result = Path.ChangeExtension(path, newExtension); + Console.WriteLine($"ChangeExtension('{path}', '{newExtension}') returns '{result}'"); - return (result); + return result; } public string Combine(string path1, string path2) @@ -105,7 +105,7 @@ public string Combine(string path1, string path2, string path3, string path4) return combination; } - public string Combine(String[] paths) + public string Combine(string[] paths) { var combination = Path.Combine(paths); Console.WriteLine($"When you combine '{paths[0]}' and '{paths[1]}' and '{paths[2]}' and '{paths[3]}', the result is: {Environment.NewLine}'{combination}'"); @@ -113,7 +113,7 @@ public string Combine(String[] paths) return combination; } - public string GetDirectoryName(string filePath) + public string? GetDirectoryName(string filePath) { var directoryName = Path.GetDirectoryName(filePath); Console.WriteLine($"GetDirectoryName('{filePath}') returns '{directoryName}'"); @@ -121,7 +121,7 @@ public string GetDirectoryName(string filePath) return directoryName; } - public ReadOnlySpan GetFileName(ReadOnlySpan filePath) + public ReadOnlySpan GetFileName(ReadOnlySpan filePath) { ReadOnlySpan fileName = Path.GetFileName(filePath); Console.WriteLine($"GetFileName('{filePath.ToString()}') returns '{fileName.ToString()}'"); @@ -153,7 +153,7 @@ public string GetFullPath(string path, string basePath) return pathResult; } - public string GetPathRoot(string fullPathString) + public string? GetPathRoot(string fullPathString) { var pathRoot = Path.GetPathRoot(fullPathString); Console.WriteLine($"GetPathRoot('{fullPathString}') returns '{pathRoot}'"); @@ -161,22 +161,31 @@ public string GetPathRoot(string fullPathString) return pathRoot; } - public void TryJoin(ReadOnlySpan path1, ReadOnlySpan path2, Span destination, out Int32 charsWritten) + public void TryJoin(ReadOnlySpan path1, ReadOnlySpan path2, Span destination, out int charsWritten) { if (Path.TryJoin(path1, path2, destination, out charsWritten)) - Console.WriteLine($"Wrote {charsWritten} characters: '{destination.Slice(0, charsWritten).ToString()}'"); + Console.WriteLine($"Wrote {charsWritten} characters: '{destination[..charsWritten].ToString()}'"); else Console.WriteLine("Concatenation operation failed."); } - public void TryJoin(ReadOnlySpan path1, ReadOnlySpan path2, ReadOnlySpan path3, Span destination, out Int32 charsWritten) + public void TryJoin(ReadOnlySpan path1, ReadOnlySpan path2, ReadOnlySpan path3, Span destination, out int charsWritten) { if (Path.TryJoin(path1, path2, path3, destination, out charsWritten)) - Console.WriteLine($"Wrote {charsWritten} characters: '{destination.Slice(0, charsWritten).ToString()}'"); + Console.WriteLine($"Wrote {charsWritten} characters: '{destination[..charsWritten].ToString()}'"); else Console.WriteLine("Concatenation operation failed."); } + public (string Combined, string Joined) CombineVersusJoin(string basePath, string fileName) + { + var combined = Path.Combine(basePath, fileName); + var joined = Path.Join(basePath, fileName); + Console.WriteLine($"Combine: '{combined}', Join: '{joined}'"); + + return (combined, joined); + } + public string Join(string path1, string path2, string path3) { var result = Path.Join(path1, path2, path3); diff --git a/csharp-classes-struct-record/PathClassCsharp/PathClassCsharp/Program.cs b/csharp-classes-struct-record/PathClassCsharp/PathClassCsharp/Program.cs index 26fb2f9ce1..0a105ffb07 100644 --- a/csharp-classes-struct-record/PathClassCsharp/PathClassCsharp/Program.cs +++ b/csharp-classes-struct-record/PathClassCsharp/PathClassCsharp/Program.cs @@ -175,6 +175,17 @@ static void Main(string[] args) } resultComponent = pathClass.Join(pathArrayComponents); + + string uploadsFolder = @"C:\uploads"; + string userFileName = @"\report.pdf"; + if (!Path.IsPathFullyQualified(uploadsFolder)) + { + //Linux StringPath + uploadsFolder = "/uploads"; + userFileName = "/report.pdf"; + } + + var (combined, joined) = pathClass.CombineVersusJoin(uploadsFolder, userFileName); } } } diff --git a/csharp-classes-struct-record/PathClassCsharp/Tests/PathClassCsharpUnitTest.csproj b/csharp-classes-struct-record/PathClassCsharp/Tests/PathClassCsharpUnitTest.csproj index bab5548dd5..168478a55c 100644 --- a/csharp-classes-struct-record/PathClassCsharp/Tests/PathClassCsharpUnitTest.csproj +++ b/csharp-classes-struct-record/PathClassCsharp/Tests/PathClassCsharpUnitTest.csproj @@ -1,7 +1,7 @@  - net6.0 + net10.0 enable enable @@ -9,11 +9,11 @@ - - - - - + + + + + diff --git a/csharp-classes-struct-record/PathClassCsharp/Tests/PathClassTest.cs b/csharp-classes-struct-record/PathClassCsharp/Tests/PathClassTest.cs index 58a2d236a4..94c9293f42 100644 --- a/csharp-classes-struct-record/PathClassCsharp/Tests/PathClassTest.cs +++ b/csharp-classes-struct-record/PathClassCsharp/Tests/PathClassTest.cs @@ -495,5 +495,61 @@ public void WhenCallingJoin_ThenJoinedPathStingIsReturned() Assert.That(resultComponent, Is.EqualTo("D:/users/user1/documents\\letters")); } } + + [Test] + public void WhenCombiningWithRootedFileName_ThenCombineDropsTheBaseAndJoinKeepsIt() + { + string basePath = @"C:\uploads"; + string fileName = @"\report.pdf"; + if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + //Linux StringPath + basePath = "/uploads"; + fileName = "/report.pdf"; + } + + var (combined, joined) = pathClass.CombineVersusJoin(basePath, fileName); + + if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + Assert.That(combined, Is.EqualTo("/report.pdf")); + Assert.That(joined, Is.EqualTo("/uploads/report.pdf")); + } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + Assert.That(combined, Is.EqualTo(@"\report.pdf")); + Assert.That(joined, Is.EqualTo(@"C:\uploads\report.pdf")); + } + } + + [Test] + public void WhenCallingTableMembers_ThenDocumentedResultsAreReturned() + { + string filePath = @"C:\MyDir\MySubDir\myfile.ext"; + string basePath = @"C:\MyDir"; + if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + //Linux StringPath + filePath = "/MyDir/MySubDir/myfile.ext"; + basePath = "/MyDir"; + } + + bool linux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux); + + Assert.That(Path.GetFileName(filePath), Is.EqualTo("myfile.ext")); + Assert.That(Path.GetFileNameWithoutExtension(filePath), Is.EqualTo("myfile")); + Assert.That(Path.GetExtension(filePath), Is.EqualTo(".ext")); + Assert.That(Path.HasExtension(filePath), Is.True); + Assert.That(Path.ChangeExtension(filePath, ".old"), + Is.EqualTo(linux ? "/MyDir/MySubDir/myfile.old" : @"C:\MyDir\MySubDir\myfile.old")); + Assert.That(Path.GetDirectoryName(filePath), + Is.EqualTo(linux ? "/MyDir/MySubDir" : @"C:\MyDir\MySubDir")); + Assert.That(Path.GetPathRoot(filePath), Is.EqualTo(linux ? "/" : @"C:\")); + Assert.That(Path.GetRelativePath(basePath, filePath), + Is.EqualTo(linux ? "MySubDir/myfile.ext" : @"MySubDir\myfile.ext")); + Assert.That(Path.IsPathFullyQualified(filePath), Is.True); + Assert.That(Path.IsPathFullyQualified("MySubDir/myfile.ext"), Is.False); + Assert.That(Path.EndsInDirectorySeparator(filePath), Is.False); + } } } \ No newline at end of file