我已经养成了在文件夹路径的末尾总是放置一个斜杠的习惯,这似乎是常态(例如,$(ProjectDir)等Visual Studio宏总是有一个尾部斜杠)。另外,当我将相对路径附加到现有文件夹路径的末尾时,我总是放置一个前导斜杠,以防传递给我的文件夹路径没有尾部斜杠(例如Windows) batch:将FULL_FILE_PATH =%FOLDER_PATH%\ path \设置为\ some \ file)。
话虽这么说,我倾向于使用看起来像C:\ path \ to \ folder \\ path \ to \ some \ file.txt的路径(注意连续的两个反斜杠)。另外,由于我使用dev \ src dev \ include和dev \ script文件夹结构(其中.vcxproj文件和类似的文件夹在脚本文件夹中),我的大多数路径都附加了一个相对路径,其中包含up-levels一些宏的结束,如$(ProjectDir)(例如Include dir = $(ProjectDir)\ .. \ include \)。
在下面的代码中,somePath02Uri和somePath03Uri返回(我认为是)不正确的结果:
var url = "http://dizievents.ch/index.php/event/food-n-liquor-dizinvolta-2/";
window.location.href = url;
为什么System.Uri将一行中的两个斜杠解释为文件夹?
几乎忘了:如果遇到这种情况,快速而肮脏的解决方案是在创建Uri对象之前从字符串中删除连续的两个反斜杠。我这样做是为了添加using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DotNet45SystemUriTest
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("We will now test the System.Uri constructor:\n");
// Output from Console.WriteLine is in the comment on the same line
String somePath01 = "C:\\some\\common\\..\\include\\";
Console.WriteLine("somePath01 = " + somePath01); // somePath01 = C:\some\common\..\include\
Uri somePath01Uri = new Uri(somePath01);
Console.WriteLine("somePath01Uri = " + somePath01Uri.ToString()); // somePath01Uri = file:///C:/some/include/
Console.WriteLine();
String somePath02 = "C:\\some\\common\\\\..\\include\\";
Console.WriteLine("somePath02 = " + somePath02); // somePath02 = C:\some\common\\..\include\
Uri somePath02Uri = new Uri(somePath02);
Console.WriteLine("somePath02Uri = " + somePath02Uri.ToString()); // somePath02Uri = file:///C:/some/common/include/
Console.WriteLine();
String somePath03 = "C:\\some\\common\\\\\\..\\include\\";
Console.WriteLine("somePath03 = " + somePath03); // somePath03 = C:\some\common\\\..\include\
Uri somePath03Uri = new Uri(somePath03);
Console.WriteLine("somePath03Uri = " + somePath03Uri.ToString()); // somePath03Uri = file:///C:/some/common//include/
Console.WriteLine();
}
}
}
:
.Replace("\\\\", "\\")
答案 0 :(得分:0)
用于非转义的两个斜杠。
例如试试这个:
string path =" c:\ test \ html&#34 ;;
当您编译这段代码时,您将收到错误"无法识别的转义序列"。
但是如果你添加一个额外的斜杠,你告诉编译器没有预期的转义,字符串应该按原样进行解析。
string path =" c:\\ test \\ html&#34 ;;
非逃避的另一种有趣的方法是:
string path = @" c:\ test \ html&#34 ;;