改变/&引用;至\&引用;[C#]

改变/&引用;至\&引用;[C#],c#,C#,我已经看到了另一种情况。但是这个我抓不住。我正在尝试获取web资源路径的一部分,并将其与本地路径相结合。 让我再解释一下 public string GetLocalPath(string URI, string webResourcePath, string folderWatchPath) // get the folderwatcher path to work in the local folder { string changedPath = webResour

我已经看到了另一种情况。但是这个我抓不住。我正在尝试获取web资源路径的一部分,并将其与本地路径相结合。 让我再解释一下

public string GetLocalPath(string URI, string webResourcePath, string folderWatchPath) // get the folderwatcher path to work in the local folder
    {
        string changedPath = webResourcePath.Replace(URI, "");
        string localPathTemp = folderWatchPath + changedPath;
        string localPath = localPathTemp.Replace(@"/",@"\");
        return localPath;
    }
但是,当我这样做的时候,结果是

C:\\Users
但我想要的是

C:\Users 
不是“\\”而是我的调试像
C:\\Users
一样显示它,但在控制台中它像我期望的那样显示它。 我想知道原因
谢谢。

因为
\
\
的转义序列

string str  = "C:\\Users";

string str  = @"C:\Users";
后面的一个称为逐字字符串文字

要在代码中组合路径,最好使用,而不是手动添加
“/”

您的代码应该是

public string GetLocalPath(string URI, string webResourcePath, 
                           string folderWatchPath)
{
    return Path.Combine(folderWatchPath, webResourcePath.Replace(URI, ""));
}

无需将
/
替换为
\
,因为windows中的路径名同时支持这两种名称。因此
C:\Users
C:/Users

相同,因为
\
\
的转义序列

string str  = "C:\\Users";

string str  = @"C:\Users";
后面的一个称为逐字字符串文字

要在代码中组合路径,最好使用,而不是手动添加
“/”

您的代码应该是

public string GetLocalPath(string URI, string webResourcePath, 
                           string folderWatchPath)
{
    return Path.Combine(folderWatchPath, webResourcePath.Replace(URI, ""));
}

无需将
/
替换为
\
,因为windows中的路径名同时支持这两种名称。因此
C:\Users
C:/Users

相同,我认为debug显示带有转义字符的字符串,而要以非逐字(不加前缀
)字符串转义
\
)字符串,您必须编写
\

我相信debug显示带有转义字符的字符串,要在非逐字(不加前缀)字符串中转义
\
,必须用C语言编写
\

\
分隔字符串中是特殊的。为了获得字符串中的文本
\
,您需要将其加倍
\
@“
字符串中并不特殊,因此
@“\”
“\\”
@“C:\Users”
“C:\\Users”
的意思完全相同。调试器显然在您的示例中使用了第二种样式。

在C#中,
\
分隔的字符串中是特殊的。为了获得字符串中的文本
\
,您需要将其加倍
\
@“
字符串中并不特殊,因此
@“\”
“\\”
@“C:\Users”
“C:\\Users”
的意思完全相同。调试器显然在您的情况下使用了第二种样式。

Windows支持路径名的格式为
C:\Users
C:/Users
。您可能根本不需要转换。Windows支持路径名的格式为
C:\Users
C:/Users
。您可能根本不需要转换。非常感谢!!谢谢你的帮助非常感谢!!谢谢你的帮助