Path.Combine 是否曾在 .net 中进行网络调用?
我认识的人声称它确实如此,我正在反编译 System.IO 并查看 Path 类,但我看不到它进行网络调用。唯一的可疑之处在于 NormalizePath,它调用 PathHelper,而 PathHelper 又调用 Win32Native.GetFullPathName。我不知道那是做什么的。
他们还声称 System.Uri 在创建时会进行网络调用,我觉得这非常令人难以置信。我简直不敢相信它会做到这一点,因为它会慢得令人难以置信,而且这些方法又是多么内在。
谁能启发我吗?
编辑: 事实证明,Path.Combine(p)
永远不会调用网络,但 Path.GetFullName(p)
可以。如果您有一个短文件名的 UNC 路径(例如 "\\server\abcdef~1.txt"
),它实际上会调用网络并尝试扩展路径,坦白说,这让我大吃一惊。
Someone I know is claiming that it does and I am decompiling System.IO and looking in the Path class and I can't see it making networking calls. The only suspect is in NormalizePath, where it calls to PathHelper which has calls into Win32Native.GetFullPathName. I don't know what that does.
They are also claiming that System.Uri makes networking calls when created, which I find very incredible. I just can't believe that it would do that given how unbelievably slow that would be and how intrinsic these methods are.
Can anyone enlighten me?
Edit:
It turns out that Path.Combine(p)
doesn't ever call the network but Path.GetFullName(p)
can. In the scenario where you have a UNC path with a short filename ("\\server\abcdef~1.txt"
for example) it will actually call out to the network and try to expand the path, which blows my mind frankly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,Path.Combine 方法只是执行所需的字符串操作来生成合法的路径字符串(给定路径分隔符)。它明确地不检查您是否为其提供了有效的路径、有效的文件名或其他内容。
如果您好奇的话,可以使用 .NET 4 的参考源代码,并且您可以看到工作完全在托管代码中完成,没有本机方法调用,并且基本上是:(
当然更加健壮和灵活,但这就是想法。)
类似地,Uri 类的构造函数主要进行字符串解析(尽管比 Path 的东西复杂几个数量级),但我仍然看不到网络调用。
您还可以通过在 C# 应用程序中执行此类命令时运行数据包捕获实用程序(例如 Wireshark)来自行检查。
No, the
Path.Combine
method simply performs the required string manipulation to generate a legal path string, given the path separator. It explicitly does not check to see if you've given it a valid path, or a valid file name, or whatever.The reference source code for .NET 4 is available, if you're curious, and you can see that the work is done entirely in managed code, no native method calls, and is basically:
(A lot more robust and flexible, of course, but that's the idea.)
Similarly, the constructors for the Uri class do mostly string parsing (though orders of magnitude more complex than the Path stuff) but still, no network calls that I can see.
You could also check this yourself by running a packet capture utility such as Wireshark while executing such commands in a C# app.