gpt4 book ai didi

c# - 在 C# 中创建文件 Uri

转载 作者:行者123 更新时间:2023-11-30 21:12:47 26 4
gpt4 key购买 nike

我想我遗漏了一些明显的东西......

我一直在阅读一个新项目中的代码并且遇到了等同于以下内容的代码:

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com