如何打开 WPF 内容流?

发布于 2024-09-02 05:10:29 字数 236 浏览 1 评论 0原文

这是代码片段

String str= ??????? // I want to assign c:/my/test.html to this string
Uri uri= new Uri (str);
Stream src = Application.GetContentStream(uri).Stream;

执行此操作的正确方法是什么?我收到“URI notrelative”异常抛出

Here's the code snippet

String str= ??????? // I want to assign c:/my/test.html to this string
Uri uri= new Uri (str);
Stream src = Application.GetContentStream(uri).Stream;

What's the correct way to do this? I'm getting "URI not relative" Exception thrown

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

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

发布评论

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

评论(4

顾挽 2024-09-09 05:10:29

您的问题特定于 WPF。请参阅 Application.GetContentStream 方法。

您将看到此方法需要相对 URI。请参阅“WPF 应用程序、资源、内容和数据文件”。

Your problem is specific to WPF. See the Application.GetContentStream method.

You'll read that this method requires a relative URI. See "WPF Application, Resource, Content and Data files".

不及他 2024-09-09 05:10:29

您有一个文件路径 - 如果您想将其设为 URI,请添加“file:///”,即。 “文件:///c:/my/test.html”

You have a file path - if you want to make it a URI add "file:///", ie. "file:///c:/my/test.html"

┊风居住的梦幻卍 2024-09-09 05:10:29

对于本地文件 URI,您需要为其添加前缀:

file:///

For local file URIs, you need to prefix it with:

file:///
Hello爱情风 2024-09-09 05:10:29

我想你会发现你的问题是 Application.GetContentStream 用于位于指定 Uri 的内容数据文件的资源流。也就是说,与可执行程序集一起部署。

如果您查看:http://msdn。 microsoft.com/en-us/library/aa970494(VS.90).aspx#Site_of_Origin_Files

您应该发现上述 file:/// 语法是正确的...但是如果您要打开它们,您可能需要某种开关来确定如何获取流:

FileInfo fileToSave;
if (!existingFile.IsFile)
    throw new ArgumentException("Input URI must represent a local file path", "existingFile");

fileToSave = new FileInfo(existingFile.LocalPath);
return fileToSave.Open(/* Args based on your needs */)

同样,如果它是一个 Web URI:

if (!existingFile.Scheme.StartsWith("http"))
    throw new ArgumentException("Input URI must represent a remote URL path", "existingFile");
// Do a WebRequest.Create call and attempt download... (Perhaps to MemoryStream for future use)

希望有所帮助。

安德鲁.

I think you'll find your problem is that Application.GetContentStream is for a resource stream for a content data file that is located at the specified Uri. That is, deployed alongside an executable assembly.

If you look at: http://msdn.microsoft.com/en-us/library/aa970494(VS.90).aspx#Site_of_Origin_Files

You should find that the file:/// syntax as stated above is correct... But if you're going to open them you'll probably want some kind of switch to work out how to get the stream:

FileInfo fileToSave;
if (!existingFile.IsFile)
    throw new ArgumentException("Input URI must represent a local file path", "existingFile");

fileToSave = new FileInfo(existingFile.LocalPath);
return fileToSave.Open(/* Args based on your needs */)

And similarly if it's a web URI:

if (!existingFile.Scheme.StartsWith("http"))
    throw new ArgumentException("Input URI must represent a remote URL path", "existingFile");
// Do a WebRequest.Create call and attempt download... (Perhaps to MemoryStream for future use)

Hope that helps.

Andrew.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文