2

If I instantiate a Uri that refers to a path where no file exists, I'd like it to throw an error but it doesn't. And nothing I do with such a Uri seems to throw an error either.

var bi = new BitmapImage(new Uri("vignette.png", UriKind.Relative));

Is there any way to get it to throw an error? Or is there a way to check if the file exists like with File.Exists() but using a Uri object instead of a string?

This is not a duplicate of C# How can I check if a URL exists/is valid? because my question pertains specifically to testing a C# Uri object. I'm already aware of how to check if a file exists using a string representation of its path.

Community
  • 1
  • 1
Kyle Delaney
  • 11,616
  • 6
  • 39
  • 66
  • 2
    Maybe this is what you're looking for? http://stackoverflow.com/questions/924679/c-sharp-how-can-i-check-if-a-url-exists-is-valid – WithMetta May 15 '17 at 19:01
  • No, I'm talking about C# `Uri` objects. Not a single `Uri` object is mentioned in that question or any of its answers. This is not a duplicate. If that's still unclear, can someone help me make it clearer? – Kyle Delaney May 15 '17 at 19:17
  • It's like Path. It just helps you build and mess around with URIs. It doesn't care if it exists. – Mike Cheel May 15 '17 at 22:21
  • So it can't be done? – Kyle Delaney May 15 '17 at 22:45
  • 2
    This can be done by creating a lightweight class that implements implicit casts operators to/from string. Then call `File.Exists(...)` in the constructor. Here's an example from one of my personal libraries: https://github.com/austindrenski/AD.IO/blob/master/AD.IO/Paths/FilePath.cs – Austin Drenski May 16 '17 at 02:35

0 Answers0