Green_qaue
Green_qaue

Reputation: 3661

Find path to file after adding it to properties

Trying to find the path to my file after adding it to properties is not working very well. In my properties the file looks like this:

internal static System.Drawing.Bitmap one {
        get {
            object obj = ResourceManager.GetObject("one", resourceCulture);
            return ((System.Drawing.Bitmap)(obj));
        }
    }

How can I find the path to this file? And then I try to use it like this:

System.Reflection.Assembly thisExe;
        thisExe = System.Reflection.Assembly.GetExecutingAssembly();
        System.IO.Stream file =
            thisExe.GetManifestResourceStream("WindowsFormsApplication1.Properties.Resources.one");
        this.pictureBox1.Image = Image.FromStream(file);

When I run this code:

System.Reflection.Assembly thisExe; 
thisExe = System.Reflection.Assembly.GetExecutingAssembly();
string [] resources = thisExe.GetManifestResourceNames();
string list = "";

// Build the string of resources.
foreach (string resource in resources)
list += resource + "\r\n";

It gives me the following paths: "WFA1.Form1.resources" and "WFA1.Properties.Resources.resources"

Can add that my recourses are embedded.

If you need any more info please let me know.

So what I want is a path to my file, or info on HOW I can find the path. After looking around they say this should work:

System.IO.Stream file =
            thisExe.GetManifestResourceStream("[WindowsFormsApplication1.Form1.resources].[a.jpg]");

IE:

System.IO.Stream file =
            thisExe.GetManifestResourceStream("[Namespace].[file and extension]");

So it seems Im getting my namespace wrong cus it still returns null at this line:

this.pictureBox1.Image = Image.FromStream(file);

Upvotes: 2

Views: 341

Answers (1)

Seany84
Seany84

Reputation: 5596

I have had a look at this and I don't believe that there's a way to get the path without some serious string building/manipulation. Which of course could lead to bigger issues.

I presume this code will not suffice:

var bmp = new Bitmap(WindowsFormsApplication1.Properties.Resources.one);
pictureBox1.Image = bmp;

Instead of this one:

var thisExe = Assembly.GetExecutingAssembly();
var file = thisExe.GetManifestResourceStream("WindowsFormsApplication1.Properties.Resources.one");
if (file != null) pictureBox1.Image = Image.FromStream(file);

Option 2:

var assembly = System.Reflection.Assembly.GetExecutingAssembly(); 
var stream =  assembly.GetManifestResourceStream("WindowsFormsApplication1.Properties.Resources.one.jpg");
var tempPath = Path.GetTempPath(); 
File.Save(stream, tempPath);

Upvotes: 2

Related Questions