0

I have multiple projects in a solution and I'd like them all to share one pool of graphics resources. I've tried this:

1 - Created project1, made its resource file public, added some graphics to it.

2 - Created project2, Alt+dragged Resources.resx from project1\Properties to project2 (not in the Properties folder)

3 - Add reference in project2 to project1

So, now all the images from project1 are available in project2. So far, so good. I can use them at design time just fine.

But, when I want to access them at runtime, I try this (in project2)...

Image img = project1.Properties.Resources.image14;

And that crashes with a MissingManifestResourceException.

What am I doing wrong here? Is there a better way I could approach this? All I'm trying to do is maintain all my graphics in one place, so if I add a resource, it becomes available to all projects in the solution.

TrespassersW
  • 403
  • 7
  • 14
  • `Alt+dragged Resources.resx`...Just add a reference to the project 1 in the project 2. – gustavodidomenico Jun 10 '14 at 21:59
  • I already have a reference. But if I don't link the Resources file then I can't access them during design time (I mean, click on a BackgroundImage property and get to the resource from the editor). – TrespassersW Jun 10 '14 at 22:11
  • I've discovered something interesting... When I just add a reference as gustavodidomenico suggested, I can use the above code just fine, but can't access the resources at design time from the editor. When I link the resource to the other project, I can access them through the editor just fine, but can't use them as in the code above. – TrespassersW Jun 10 '14 at 23:03

2 Answers2

4

Just built an example following these steps:

  1. Create a class library do hold the resources (Project 1)
  2. Create the consumer project (Project 2)
  3. Add a resource file (GlobalResources.resx) in the Project 1 and add a resource item Information
  4. Change the BuildAction of the resource file to Embedded Resource
  5. Change the Do not copy of the resource file to False
  6. Check if the Custom Tool of the resource file is set to PublicResXFileCodeGenerator
  7. Add a reference to the class library (Project 1) to the consumer project (Project2).
  8. Add the resource namespace reference wherever you want to use it.
  9. Finally it is working: GlobalResources.Information

It should be simple.

Edit:

You are concerned about using an external resource file inside the design time property editor. Sorry to inform that there is no standard support for this :(

However, if you think that the benefits are greater than the effort:

Issue with shared WinForms resources across projects in Visual Studio

How do I get the Windows Forms Designer to use resources from external assembly?

Hope it helps.

Community
  • 1
  • 1
gustavodidomenico
  • 4,640
  • 1
  • 34
  • 50
  • I've done exactly what you said. Now, go to Project 2, click to edit the BackgroundImage of the form, and try to set it to a graphic found in GlobalResources. Can't do it because GlobalResources isn't there. AFAICT, it doesn't show up in the editor unless it is actually included in the project (not just a reference). That is why I was linking it in. However, as I said, if I link it in, it shows up in the editor just fine, but I can no longer use it at runtime as the original post indicated. Can this be set up so I can use the resources at design time and at runtime? – TrespassersW Jun 11 '14 at 14:58
  • That's what I'm talking about. Thanks a ton, gustavodidomenico! – TrespassersW Jun 11 '14 at 16:23
  • Nice intonation :). Yw! – gustavodidomenico Jun 11 '14 at 16:33
0

Choose the referenced file in your solution explorer, then properties, then see what the "copy to output" property looks like. I suspect it's not set to "Copy Always" or "Copy if Newer" of which either should be fine.

Once it's being copied, let's also check to see where it's being copied. Is the output path for that particular item the same as where the program ultimately expects? Is it being copied to the bin\Debug of the correct project?

Make sure it's being copied to the path where the MissingManifestResourceException says it's failed to find the resource.

Finally, given additional information in our comments, I would also suggest you verify the following:

  1. culture the resources are targeting. Check spelling and capitalization.
  2. any culture settings of your build xml or publish xml.
  3. culture setting(s?) of your host system that's running this code.
clarkitect
  • 1,720
  • 14
  • 23
  • Well, you were right. It was set to "Do Not Copy". But changing it didn't seem to do anything. Maybe this is my ignorance shining through, but I'm not sure why copying an embedded resource file to my output folder would help. – TrespassersW Jun 10 '14 at 22:35
  • I'm not sure we're on the same page here. But the exception says "Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "project1.Properties.Resources.resources" was correctly embedded or linked into assembly "project2" at compile time, or that all satellite assemblies required are loadable and fully signed." I should qualify, this is a runtime error. It compiles just fine. These are embedded resources that compile in OK, but then I can't seem to access through the generated Resources object. They shouldn't need to be in my bin folder. – TrespassersW Jun 10 '14 at 22:56
  • Agreed, that additional information suggests that they're compiled in under the wrong culture or that your computer is set to a different culture. Does the resources file not match the culture that your computer has selected? – clarkitect Jun 11 '14 at 00:08
  • Everything is for the neutral culture. The embedded resource works fine within project1. It's when I try to get to it from project2 that things go wrong. – TrespassersW Jun 11 '14 at 13:41