Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I want to know how to open a file(all type) from resource manager(i already added files in resource manager..But i want to know how to open when I click a button(EX. Picture or anything that can be clicked)
Posted

You find the resources in the class Resources. Given the top-level namespace of your project is called "MyProject", you find the default Resources-class under the namespace MyProject.Properties, e.g.:
C#
Bitmap image1 = MyProject.Properties.Resources.Image1;

Depending on the type of resource the members of the Resources-class also have different types: String, Bitmap, Icon, byte[], etc.

You can take a look at it by navigating to the definition of the Resources-class or by opening the file "Resources.Designer.cs" from: Solution Explorer -> YourProject -> Properties -> Resources.resx.

If you add custom resource-files (via Add -> New Item -> Resources File) they can have custom names and can be located under custom namespaces.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 10-May-15 17:18pm    
I would add the the inquirer can find it in an auto-generated file shown in the Solution Explorer node, a child node of the resx node. Many beginners don't even try to look there, by some weird reasons. Also, the exact namespace depends on the location of this node and default namespace. It just could be explained a bit better. (Voted 4 this time.)
—SA
Sascha Lefèvre 10-May-15 18:54pm    
Thank you for your comment and vote, Sergey. I agree, I've included this into the solution.
Sergey Alexandrovich Kryukov 10-May-15 20:15pm    
Appreciate it...
—SA
You can also use reflection to access embedded resources using a method similar to this:
C#
public static Stream GetResourceStream (string itemPath)
{
    return Assembly.GetCallingAssembly().GetManifestResourceStream(itemPath);
}


You can then create an image from the stream:
C#
new Bitmap(GetResourceStream(itemPath));


TIP
Also, make sure you set the compile type to "Embedded Resource" in Visual Studio for this to work.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900