In your code, I don't think you want to restrict the catch to "file not found" 
exceptions.  Suppose the satellite resource file exists, but there's no 
resource with that name in it (or the resource isn't an image)?  You don't want 
the failure to terminate the app, do you?

I think it would make sense to have try / catch around the attempt to read the 
resource from the main assembly.  (You could then rearrange the code so you 
only call GetExecutingAssembly if you're actually going to use the result.)  If 
you don't find the named image in the main assembly, you could return a 
"placeholder" image -- one that perhaps is the little red-x-in-a-box that 
browsers show when an image isn't found.

  private static Image TryLoadLocalizedImageResource(string name)
  {
    System.Globalization.CultureInfo curruicult =
      System.Threading.Thread.CurrentThread.CurrentUICulture;

    try
    {
      System.Reflection.Assembly satellite =
        currassem.GetSatelliteAssembly(curruicult);
      return Image.FromStream(satellite.GetManifestResourceStream(name));
    }
    catch {}
  // We haven't returned, so there must have been an exception.
    System.Reflection.Assembly currassem =
      System.Reflection.Assembly.GetExecutingAssembly();
    try
    {
       return Image.FromStream(currassem.GetManifestResourceStream(name));
    }
    catch {}
  // We haven't returned, so there must have been an exception.
    try
    {
       return 
Image.FromStream(currassem.GetManifestResourceStream('MissingImage'));
    }
    catch
    {
       return null; // or throw an exception naming both <name> and 
MissingImage ??
    }
  }

Good luck...

At 06:18 PM 12/1/2004, Shawn A. Van Ness wrote (in part)

>So, instead of using ResourceManager to load this kind of binary goo,
>I've written a few lines of helper code to grab it out of the
>appropriate satellite assem (failing back to the main assem, if
>necessary):
>
>  private static Image TryLoadLocalizedImageResource(string name)
>  {
>    System.Globalization.CultureInfo curruicult =
>      System.Threading.Thread.CurrentThread.CurrentUICulture;
>
>    System.Reflection.Assembly currassem =
>System.Reflection.Assembly.GetExecutingAssembly();
>
>    try
>    {
>      System.Reflection.Assembly satellite =
>currassem.GetSatelliteAssembly(curruicult);
>      return Image.FromStream(satellite.GetManifestResourceStream(name));
>    }
>    catch (System.IO.FileNotFoundException)
>    {
>      return Image.FromStream(currassem.GetManifestResourceStream(name));
>    }
>  }
>
>Thanks for all the pointers, and the moral support, everyone...!
>
>Cheers,
>-S


J. Merrill / Analytical Software Corp

===================================
This list is hosted by DevelopMentor�  http://www.develop.com
Some .NET courses you may be interested in:

Essential .NET: building applications and components with C#
November 29 - December 3, in Los Angeles
http://www.develop.com/courses/edotnet

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to