为什么不这样Path.Combine工作? [英] Why won't this Path.Combine work?

查看:137
本文介绍了为什么不这样Path.Combine工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下命令:

string reportedContentFolderPath =
Path.Combine(contentFolder.FullName.ToString(), @"\ReportedContent\");

当我看到在调试器,我可以看到以下内容:

When I look in the debugger I can see the following:

contentFolder.FullName = "E:\\"

然而

reportedContentFolderPath = "\\ReportedContent\\"

为什么 Path.Combine 斩去E:\?

推荐答案

您对 A斜线@\ReportedContent\。你不希望出现这种情况(或后一个,我怀疑) - 尝试只:

You have a leading slash on @"\ReportedContent\". You don't want that (or the trailing one, I suspect) - try just:

string reportedContentFolderPath =
    Path.Combine(contentFolder.FullName.ToString(), "ReportedContent");



从的the文档

如果路径2 不包括根目录(例如,如果 PATH2 不与分隔符或一个驱动器规范开始),其结果是两个路径的串联,与居间分隔符。如果路径2 包括根,路径2 被返回。

If path2 does not include a root (for example, if path2 does not start with a separator character or a drive specification), the result is a concatenation of the two paths, with an intervening separator character. If path2 includes a root, path2 is returned.

在你的情况,路径2 没有的包含一个根,所以这是不看<$ C $返回C>路径1 。

In your case, path2 did contain a root, so it was returned without looking at path1.

这篇关于为什么不这样Path.Combine工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆