在 ASP.NET MVC 应用程序中,我尝试连接两个路径,一个远程服务器路径与一个从数据库提取的路径。我正在执行以下操作:
string serverPath = @"\\myServer\TempFolder";
string filePath = GetPathFromDatabaseTable();
string finalPath = System.IO.Path.Combine(serverPath, filePath);
GetPathFromDatabaseTable 方法返回此字符串:
\\path\\to\\file.pdf
当使用Path.Combine连接时,进入finalPath的结果是:
\\path\\to\\file.pdf
因此前缀 serverPath\myServer\TempFolder 被删除。为什么会发生?
最佳答案
您可以使用Uri
类来实现远程和本地路径的组合:
string serverPath = @"\\myServer\TempFolder";
string filePath = "\\path\\to\\file.pdf";
Uri serverUri = new Uri(serverPath + filePath);
string finalPath = serverUri.LocalPath;
哪个返回
\\myserver\TempFolder\path\to\file.pdf
关于c# - 将远程服务器路径与文件路径组合时 Path.Combine 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61349703/