Sharing Resources between a Web and a Windows Forms Application

It is possible to share a common set of resource files between a web and a windows forms application quite easily. You first create the resource files on the web application as global resource files, and place them on the App_GlobalResources folder. On the windows application, add a link to these files, that is, do not copy them. Make sure you mark them as embedded resources. Here comes the tricky part: you can see from the generated classes that these classes are trying to load from the App_GlobalResources folder, what you have to do is, through reflection, change the resource manager to point to the current assembly and resource location. Let's see how this is done: FieldInfo fi = typeof(MyResourceClass).GetField("resourceMan", BindingFlags.Static | BindingFlags.NonPublic); fi.SetValue(null, new ResourceManager("My.Full.Default.Assembly.Namespace.MyResourceClass", Assembly.GetExecutingAssembly()); In this example, suppose your windows forms assembly has a default namespace of My.Full.Default.Assembly.Namespace. And that's it! You can now do: String myVariable = MyResourceClass.MyResource on both projects.

                             

No Comments