c# - 将远程服务器路径与文件路径组合时 Path.Combine 不起作用

标签 c# asp.net-mvc visual-studio-2013 path-combine

在 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/

相关文章:

c# - 你打算如何声明一个依赖属性?

c# - set的封装更有效

c# - 在 .NET Core 2 Web Api 中验证 JWT 后获取用户数据的最佳实践是什么?

sql-server - 如何实现记录更改的自动时间戳 (SQL Server)

c# - XNA Game Studio 4.0 未显示在已安装的模板中

c# - 在 C# 中使用 discriminated-union 的 Match 表示状态转换时,如何执行循环状态转换?

c# - 非 Controller 类无法访问 DbContext

asp.net-mvc - 使用 ASP.NET MVC 的 SEO URL

c++ - 如何在 Visual C++ 2013 上递归删除二叉树?

c++ - 无法在 DLL 上锁定 C++ 11 std::mutex