我已经看到了另一种方式。但是这一点我抓不到。我正在尝试获取Web resourcePath的一部分,并将其与本地路径结合起来。
让我解释一下。
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";
后来的一个被称为Verbatim字符串文字。
为了在代码中合并路径,最好使用
Path.Combine
而不是手动添加"/"
您的代码应该像
public string GetLocalPath(string URI, string webResourcePath,
string folderWatchPath)
{
return Path.Combine(folderWatchPath, webResourcePath.Replace(URI, ""));
}
无需将
/
替换为\
,因为Windows中的路径名支持这两种。因此C:\Users
与C:/Users
相同
关于c# - 将“/”更改为“\” [C#],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10910401/