Tuesday, 20 August 2013

Loading a user control using GetManifestResourceStream

Loading a user control using GetManifestResourceStream

I have a ASP.Net C# (.NetFramework v4.0) WebForms application, which I
compiled and contains a user control.
I want to open the user control using GetManifestResourceStream().
Let us say the DLL is MyWebApp.dll. That would make
String "/bin/MyWebApp.dll"; System.Reflection.Assembly assembly =
System.Reflection.Assembly.LoadFile(assemblyName);
I can load the file with no issues.
Let us assume the following directory structure for the project.
/Controls/MyUserControl1.ascx
There is obviously a C# file associated with that as well.
I marked the ascx file Build Action as "Embed Resource".
I then specify the following:
String resourceName = "Controls/MyUserControl1.ascx"; System.IO.Stream
oStream = assembly.GetManifestResourceStream(resourceName);
Visual Studio 2012 throws an error: "Value cannot be null.\r\nParameter
name: stream"
The actual code does not have "oStream = " but rather "return
GetManifestResourceStream...".
I know that the code should work, so I am doing something dumb, just not
sure what. The larger project is to be able to use the user control from
one assembly inside another web application. See
LoadControl(string) in different assembly?
for more information and details. The answer in that StackOverflow.com
question points to
http://www.cmswire.com/cms/tips-tricks/aspnet-reusing-web-user-controls-and-forms-000915.php
Is it possible that there is a "/", "\", "." difference somewhere, so
maybe "/Controls/MyUserControl1.ascx". I tried a few permutations, just
could not get the stream to load.

No comments:

Post a Comment