Reading a Resource Stream

J

Jeronimo Bertran

Hello ,

I am trying to read a resource in my .net app using a stream. I found the
GetMainfestResourceStream but I am having trouble using it with the
resources I edit with the resource editor. I added an icon named
Devices.ico which I need to read as a stream....


I am able to load the resource using the ResourceManager as follows:


Assembly assembly = Assembly.GetExecutingAssembly();

ResourceManager rm = new ResourceManager ("Namespace.Properties.Resources",
assembly);

// This works ok
myObj = rm.GetObject("Devices");


Stream reader = assembly.GetManifestResourceStream
("Namespace.Properties.Resources.Devices.ico");


returns a null stream... I also tried with no extension.

Thanks,

Jeronimo
 
J

Jon Skeet [C# MVP]

Jeronimo Bertran said:
I am trying to read a resource in my .net app using a stream. I found the
GetMainfestResourceStream but I am having trouble using it with the
resources I edit with the resource editor. I added an icon named
Devices.ico which I need to read as a stream....


I am able to load the resource using the ResourceManager as follows:


Assembly assembly = Assembly.GetExecutingAssembly();

ResourceManager rm = new ResourceManager ("Namespace.Properties.Resources",
assembly);

// This works ok
myObj = rm.GetObject("Devices");


Stream reader = assembly.GetManifestResourceStream
("Namespace.Properties.Resources.Devices.ico");

returns a null stream... I also tried with no extension.

Try using GetManifestResourceNames() to find all the names. I suspect
you need to use slashes (not sure which kind!) in the name instead of
dots (except for the extension).
 
P

Peter Huang [MSFT]

Hi

Based on my research, you may follow the steps to see if that works for you.
1. Check if you have set the icon file's Build Action to Embedded Resource.
2. Run the ass.GetManifestResourceNames() to enumerate what is the exact
string in the assembly.
3. use that string to call GetManifestResourceStream

Here is my test snippet which works at my side.

private void button2_Click(object sender, EventArgs e)
{
Assembly ass = Assembly.GetExecutingAssembly();
foreach (string s in ass.GetManifestResourceNames())
Debug.WriteLine(s); // From here I found the string for the
icon is "TestWinform.MyIcon.ico"

Stream stm =
ass.GetManifestResourceStream("TestWinform.MyIcon.ico");
Icon = new Icon(stm);
}

You may have a try and let me know the result.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jeronimo Bertran

Peter

Thanks, I tried it and it does show the resources that are embedded on the
assembly, but I am still not able to see the resources that are added to
the Resources.resx file.


Assembly assembly = Assembly.GetExecutingAssembly();
foreach (string s in assembly.GetManifestResourceNames())
Debug.WriteLine(s);

The output list includes the following:


MyApp.Properties.Resources.resources


And I want to read an icon file that is inside this resource.

Thanks again.
 
P

Peter Huang [MSFT]

Hi Jeronimo,

Have you checked the Build Action? So that it will be built into the
Assembly.
1. Check if you have set the icon file's Build Action to Embedded Resource.

If that still did not work for you?
Can you provide a simple reproduce sample including the project files and
the icon file?
You may reach me by removing the "online" from the email address.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
P

Peter Huang [MSFT]

Hi Jeronimo,

Based on my review your code, the Device icon is loaded OK but not for the
netTerminal.ico.
Because the netTerminal.ico's Build Action is None, we need to change it
into Embedded Resource.
So that it will be built into the Assembly, because we are retrieving
resource from the Assembly.
The GetManifestResourceStream will only recognize the values get from
GetManifestResourceNames.
Because there is no such a path
ResourceLoader.Properties.Resources.netTerminal.ico, so the load is failed.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jeronimo Bertran

Peter.

So basically, even it the icon is embedded in the exe through the
Porperties.Resources.resx file, we need to have an additional copy embedded
in the exe in order to get the stream for the resource.

Thanks again.
 
P

Peter Huang [MSFT]

Hi

You are welcomed!
Yes, for a resource embedded in the resx file, we need to use Resource
manager to retrieve it.
internal static System.Drawing.Icon netTerminal {
get {
object obj = ResourceManager.GetObject("netTerminal",
resourceCulture);
return ((System.Drawing.Icon)(obj));
}
}

This is why we can get the file with the code line below.
Properties.Resources.netTerminal

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top