如何打开WPF内容流?

时间:2010-05-18 23:50:53

标签: c# wpf stream uri

这是代码段

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不相对”抛出异常

4 个答案:

答案 0 :(得分:2)

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

您会看到此方法需要相对URI。请参阅“WPF Application, Resource, Content and Data files”。

答案 1 :(得分:1)

你有一个文件路径 - 如果你想把它作为一个URI添加“file:///”,即。 “文件:/// C:/my/test.html”

答案 2 :(得分:0)

对于本地文件URI,您需要在其前面加上:

file:///

答案 3 :(得分:0)

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

如果你看一下:http://msdn.microsoft.com/en-us/library/aa970494(VS.90).aspx#Site_of_Origin_Files

您应该会发现上面提到的file:///语法是正确的......但是如果您要打开它们,您可能需要某种切换来解决如何获取流:< / p>

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 */)

同样如果它是一个网址:

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)

希望有所帮助。

安德鲁。