[C#] Resources

Newbie Spellweaver
Joined
Jun 12, 2011
Messages
95
Reaction score
35
I need a way to copy an embedded resource to somewhere on the local machine.

File.Copy() won't work as parameter 1 needs to be a directory.

I've tried using FileStream and things, but no luck.
 
There's a simple way of doing this. You can simply copy and paste this and modify it to your liking.

PHP:
byte[] myresource = Properties.Resources.myresource;
File.WriteAllBytes(Path you want here + "\\filename you want it to be.extension", my resource);

Cheers!
 
Where you're obtaining a "Properties" object from is beyond me, but it's clear you didn't understand the sample code you ripped off.

PHP:
using System.Reflection

namespace AngraMainyu
{
    public static class ResourceLoader
    {
        public static Stream GetLocalResourceStream(string name)
        {
            // The Visual C# compiler always prepends the assembly's short name to resource data
            // ie a module "MyModule" containing the resource "MyResource" stores the resource as
            // "MyModule.MyResource"
            var fullName = new AssemblyName(Assembly.GetExecutingAssembly().FullName).Name + name;
            return Assembly.GetExecutingAssembly().GetManifestResourceStream(fullName);
        }
    }
}
 
Where you're obtaining a "Properties" object from is beyond me, but it's clear you didn't understand the sample code you ripped off.

PHP:
using System.Reflection

namespace AngraMainyu
{
    public static class ResourceLoader
    {
        public static Stream GetLocalResourceStream(string name)
        {
            // The Visual C# compiler always prepends the assembly's short name to resource data
            // ie a module "MyModule" containing the resource "MyResource" stores the resource as
            // "MyModule.MyResource"
            var fullName = new AssemblyName(Assembly.GetExecutingAssembly().FullName).Name + name;
            return Assembly.GetExecutingAssembly().GetManifestResourceStream(fullName);
        }
    }
}

So, let me get this straight.
You come into this thread, insult my intelligence and understanding, while you didn't understand what Fustigate what asking for?
Infraction worthy? I think so.

Look. He didn't want to launch and embedded resource. Embedded applications require to be C# inside a C# solution.

He wanted to take any random "item" if you will, and export it from the program, and write it to the disk.

This "sample code" I apparently "ripped off" was made by me.

Nathan - [C#] Resources - RaGEZONE Forums


I wrote it for a program I made years ago. It's efficient and it works.

To reach the embedded resources of your program in C#, you need to go through the Properties of it.

Now, before you go making a fool of yourself even further, make sure you comprehend the questions first, and know of methods such as these.

- XZeenon.
 

Attachments

  • coolstorybro - [C#] Resources - RaGEZONE Forums
    coolstorybro.webp
    16.1 KB · Views: 157
So, let me get this straight.
You come into this thread, insult my intelligence and understanding, while you didn't understand what Fustigate what asking for?
Infraction worthy? I think so.


If you're so thin-skinned that you think infracting users for implying that you don't know what you're doing is a reasonable response, Mental has misplaced his trust, I think. As a user, I don't report moderators whenever they take relatively innocuous stabs at my intelligence, in part because this is the internet and nothing posted here could possibly matter any less, and in part because this is RageZone and I expect a mutual policy among adults that we all refrain from crying over hurt feelings.


XZeenon said:
Look. He didn't want to launch and embedded resource. Embedded applications require to be C# inside a C# solution. He wanted to take any random "item" if you will, and export it from the program, and write it to the disk.


Who's launching an embedded module? My code takes the name of a local resource and returns a stream pointing to the data. Writing it to the disk is a matter of making a call to File.Create and Stream.Copy, which I presume from the OP shouldn't be a problem.


XZeenon said:
This "sample code" I apparently "ripped off" was made by me.

Nathan - [C#] Resources - RaGEZONE Forums


I wrote it for a program I made years ago. It's efficient and it works.

To reach the embedded resources of your program in C#, you need to go through the Properties of it.

Now, before you go making a fool of yourself even further, make sure you comprehend the questions first, and know of methods such as these.

- XZeenon.


That it may, but that code won't work anywhere else. The Properties object is IDE-generated code, not part of the .NET framework. It only exists within your project, and unless the OP is working with a WinForms project, that code isn't automatically generated for embedded resources as far as I'm aware. I understand the method, but unless we're assuming the OP knows how to obtain the Properties object, you've effectively just instructed him to call a nonexistent method.
 
If you're so thin-skinned that you think infracting users for implying that you don't know what you're doing is a reasonable response, Mental has misplaced his trust, I think. As a user, I don't report moderators whenever they take relatively innocuous stabs at my intelligence, in part because this is the internet and nothing posted here could possibly matter any less, and in part because this is RageZone and I expect a mutual policy among adults that we all refrain from crying over hurt feelings.

For one, I didn't infract you. I just felt like it'd be acceptable anyway. A warning is not an infraction, don't get those wrong. Cool, just because I gave my own opinion on something I felt was infraction worthy, I know better, so I didn't. See, I didn't cry. Did I? I pointed out what you did wrong. It's my job. You should seriously refrain from making assumptions.
See, this post right here could get you infracted.

I'm done discussing this, and we definitely shouldn't on this thread. If you'd like to discuss this further, PM me.
 
[Outsiders perspective]

_code
_reasonable response, another attempt at the same method
_highly defensive, 'wtf are you doing' response
_response, clearly defining (insulting only as a comeback) what user meant
_'diplomatic', "i see where i went wrong" response, backing away.

Ah, forums are a marvel.
 
I still can't get it to work, and how to get it to work via FS.

Can you give me an example as if the file I wanted to write is "file"?
 
The line
Code:
var fullName = new AssemblyName(Assembly.GetExecutingAssembly().FullName).Name + name;
should be
Code:
var fullName = new AssemblyName(Assembly.GetExecutingAssembly().FullName).Name + "." + name;

My mistake.
 
The line
Code:
var fullName = new AssemblyName(Assembly.GetExecutingAssembly().FullName).Name + name;
should be
Code:
var fullName = new AssemblyName(Assembly.GetExecutingAssembly().FullName).Name + "." + name;

My mistake.

I can't figure out how to use a filestream to create this file on the local machine with what you're using.

Would I try write it as a byte or?


Could you give me a little example for me to work off?
 
PHP:
using System;
using System.IO;
using System.Reflection;

namespace ResourceLoader
{
    class Program
    {
        static void Main(string[] args)
        {
			// Dump the file in the working directory
            using (var resourceOutput = File.Create(Directory.GetCurrentDirectory() + "/file")) {
                using(var resourceStream = ResourceLoader.GetLocalResourceStream("file")) {
                    resourceStream.CopyTo(resourceOutput);
                }
            }
        }
    }
    public static class ResourceLoader
    {
        public static Stream GetLocalResourceStream(string name)
        {
            // The Visual C# compiler always prepends the assembly's short name to resource data
            // ie a module "MyModule" containing the resource "MyResource" stores the resource as
            // "MyModule.MyResource"
            var fullName = new AssemblyName(Assembly.GetExecutingAssembly().FullName).Name + "." + name;
            return Assembly.GetExecutingAssembly().GetManifestResourceStream(fullName);
        }
    } 
}
 
Eh, I fixed it.

I just made it a text file and then I'm going to write the text file to a file. (WriteAllText).

Thanks for your help though. :)
 
Back