为什么 IsWellFormedOriginalString 在文件 Uris 上失败?

发布于 2024-08-26 01:26:13 字数 683 浏览 6 评论 0原文

我有这样的代码:

string uriString = @"C:\Temp\test.html";
Uri uri = new Uri(uriString);
bool goodCond = uri.IsWellFormedOriginalString();

但是 goodCond 是假的!我做错了什么?


编辑: 谢谢约翰内斯和卡特德。我将重点关注我的问题:如何将有效文件路径转换为有效文件 Uri(使用 uri.IsWellFormedOriginalString 作为 Uri 有效性的指示)? 看看这个:

        DirectoryInfo di = new DirectoryInfo(@"c:\temp");
        FileInfo [] fis = di.GetFiles("test.html");
        FileInfo fi = fis[0];
        string uriString = fi.FullName;
        Uri uri = new Uri(uriString);
        bool goodCond = uri.IsWellFormedOriginalString()

显然 fi.fullName 是一个格式良好的路径,但仍然是 goodCond 是坏的!

I have code like this:

string uriString = @"C:\Temp\test.html";
Uri uri = new Uri(uriString);
bool goodCond = uri.IsWellFormedOriginalString();

But goodCond is false! What am I doing wrong?


Edit:
Thanks Johannes and Catdirt. I'll focus my question: How do I convert a valid file path to a valid file Uri (using uri.IsWellFormedOriginalString as an indication to the validity of the Uri)?
Take a look at this:

        DirectoryInfo di = new DirectoryInfo(@"c:\temp");
        FileInfo [] fis = di.GetFiles("test.html");
        FileInfo fi = fis[0];
        string uriString = fi.FullName;
        Uri uri = new Uri(uriString);
        bool goodCond = uri.IsWellFormedOriginalString()

Obviosly fi.fullName is a well formed path, but still goodCond is bad!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

じ违心 2024-09-02 01:26:13

您的 URI 格式不正确。

一个格式良好的示例是 file:///C:/Temp/test.html

PS Home:> (new-object Uri 'file:///C:/Temp/test.html').IsWellFormedOriginalString()
True
PS Home:> (new-object Uri 'file:///C:\Temp\test.html').IsWellFormedOriginalString()
False
PS Home:> (new-object Uri 'C:\Temp\test.html').IsWellFormedOriginalString()
False
PS Home:> (new-object Uri 'C:/Temp/test.html').IsWellFormedOriginalString()
False

Your URI is not well-formed.

A well-formed example would be file:///C:/Temp/test.html.

PS Home:> (new-object Uri 'file:///C:/Temp/test.html').IsWellFormedOriginalString()
True
PS Home:> (new-object Uri 'file:///C:\Temp\test.html').IsWellFormedOriginalString()
False
PS Home:> (new-object Uri 'C:\Temp\test.html').IsWellFormedOriginalString()
False
PS Home:> (new-object Uri 'C:/Temp/test.html').IsWellFormedOriginalString()
False
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文