tags:

views:

248

answers:

4

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

+1  A: 

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

Evgeny
Thanks. But, I get a "URI not relative" exception with that
roshan
A: 

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

file:///
Dillie-O
+1  A: 

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".

John Saunders
Thanks. This is what I need
roshan
A: 

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.

Reddog
Thanks. Now I'm having a COM nightmare(too much plumbing to do a simple task). Let me back that up even more of what exactly I'm looking for. I'm writing a tool that converts a .chm file to a .pdf file that fits into an Amazon kindle DX.For that I need a simple Browser control which loads the .html files (unzipped by the chmdecompiler) to see if it fits 6 X 8 size (most .html dynamically fits the size except the ones with <pre> tag or with a large image).
roshan
@roshan - Sorry mate, that one's one you'll have to figure out...
Reddog