1

How can I check if a Uri (file) exists in a referenced project?

Per this answer I've tried catching a FileNotFound exception but it isn't throwing any exceptions at all

string x;

try {
    var Uri = new Uri("/FamFamFam.Flags.Wpf;component/Images/some_wrong_file.png",
        UriKind.Relative);
    x = "a";
}

catch(Exception) {
    x = "b";
}

MessageBox.Show(x);

Per an MSDN thread, I've tried checking if the resultant Bitmap is null, but it always shows 'a':

string x;

try {
    var Bitmap = new BitmapImage(new Uri(
        "/FamFamFam.Flags.Wpf;component/Images/some_wrong_file.png", UriKind.Relative));
    Flag_Image.Source = Bitmap ?? throw new Exception();
    x = "a";
}

catch(Exception) {
    Flag_Image.Source = new BitmapImage(new Uri(
        "/FamFamFam.Flags.Wpf;component/Images/ca.png", UriKind.Relative));
    x = "b";
}

MessageBox.Show(x);
Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
  • In code behind, you should write the full Pack URI, including its prefix. It's also not a relative URI: `new Uri("pack://application:,,,/FamFamFam.Flags.Wpf;component/Images/some_wrong_file.png")` – Clemens Feb 24 '19 at 15:38
  • @Clemens Nice, that worked - thanks! Something so simple.. – Danny Beckett Feb 24 '19 at 15:56

0 Answers0