How do I open a WPF content stream?

Here's a 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 is the correct way to do this? I am getting "URI is not relative" Exception thrown

+2


a source to share


4 answers


Your problem is specific to WPF. See Method Application.GetContentStream

.



You will read that this method requires a relative URI. See WPF Files, Resources, Content, and Data .

+2


a source


You have the path to the file - if you want to make it URI add "file: ///", i.e. "File: /// C: /my/test.html"



+1


a source


For local file URIs, you need the prefix:

file:///

      

0


a source


I think you will find that your problem is that Application.GetContentStream is meant to stream resources for the content of the data file that is in the specified Uri. That is, it is deployed along with the 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 above is correct ... But if you are going to open them, you probably need some kind of switch to figure 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 it helps.

Andrew.

0


a source







All Articles