我想我错过了一些明显的东西......
我一直在阅读新项目中的代码,并遇到了与以下内容等效的代码:
var filePath = @"C:\temp\tmp1234.tmp"; // Generally obtained from System.IO.* calls
var uri = new Uri("file://" + filePath);
我认为这相当于:
var filePath = @"C:\temp\tmp1234.tmp";
var uri = new Uri(filePath);
(假设 Uri 类可以解析文件路径,推断方案并执行转义所需的任何操作)。
由于代码似乎可以工作,而且执行得如此频繁且如此一致,我假设原始开发人员有充分的理由使用 "file://"+ filePath
习惯用法。有人可以告诉我这个原因可能是什么吗?
谢谢!
最佳答案
它们都生成完全相同的 Uri
对象,除了(显然)OriginalString
。
您可以在 LINQPad 中看到这一点:
var filePath = @"C:\temp\tmp1234.tmp"; // Generally obtained from System.IO.* calls
new []{
new Uri("file://" + filePath),
new Uri(filePath)
}.Dump();
我猜最初的开发者并没有意识到这一点。
关于c# - 在 C# 中创建文件 Uri,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7234913/