Console.WriteLine(System.IO.Path.Combine("C:\\some\\path\\", "this\\folder\\"));在微軟的 try-dotnet 頁面中運行時,回傳C:\some\path\/this\folder\.
我希望它回來C:\some\path\this\folder\。
如何解決這個問題?
編輯:然而,在dotnetfiddle.net中,我得到了預期的結果。但是我很擔心,因為官方的 try-dotnet 回傳了意外的結果。
uj5u.com熱心網友回復:
這是來自https://docs.microsoft.com/en-us/dotnet/api/system.io.path.combine?view=net-5.0的示例
try-dotnet的問題在于它是基于 Unix 的系統。
string[] paths = {@"d:\archives", "2001", "media", "images"};
string fullPath = Path.Combine(paths);
Console.WriteLine(fullPath);
paths = new string[] {@"d:\archives\", @"2001\", "media", "images"};
fullPath = Path.Combine(paths);
Console.WriteLine(fullPath);
paths = new string[] {"d:/archives/", "2001/", "media", "images"};
fullPath = Path.Combine(paths);
Console.WriteLine(fullPath);
// The example displays the following output if run on a Windows system:
// d:\archives\2001\media\images
// d:\archives\2001\media\images
// d:/archives/2001/media\images
//
// The example displays the following output if run on a Unix-based system:
// d:\archives/2001/media/images
// d:\archives\/2001\/media/images
// d:/archives/2001/media/images
uj5u.com熱心網友回復:
您可以在列印之前宣告變數的路徑或使用路徑的值
var fullPath = "C:\\some\\path" "this\\folder\\";
Console.WriteLine(System.IO.Path.GetFileName(fullPath));
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/355956.html
