2

Well In my C# project I add a .xml file to the resources, I want it to be extracted/copied from it to the application path, I was trying to doing this:

string appPath = Path.GetDirectoryName(Application.ExecutablePath);//Declaration of the apppath
File.Copy(appPath, Properties.Resources.config);//process for copy

But is not working :/, how can I do what I want?

Derezzed
  • 1,093
  • 3
  • 11
  • 15

1 Answers1

6

Make sure the build action on your resource is set to "embed resource".

var assembly = Assembly.GetExecutingAssembly();
// See what resources are in the assembly (remove from the final code)
foreach (var name in assembly.GetManifestResourceNames()) {
    Console.Writeline("'{0}'", name);
}
using (var inputStream = assembly.GetManifestResourceStream(resourcePath)) {
    using( var outStream = File.OpenWrite(copyToPath)) {
        input.CopyTo(outStream);
    }
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 1
    @Derezzed Can you explain what isn't working? Are you getting an exception? What resource path do you pass to `GetManifestResourceStream`? Did you set "embed resource"? – Sergey Kalinichenko Jan 10 '12 at 04:05
  • Where it says "ReourcePath", I made it "Properties.Resources.config", and "copyToPath" "appPath". And yes, the config.xml resource was as a embedded resource. – Derezzed Jan 10 '12 at 04:29
  • @Derezzed The name of the resource is incorrect: it should be a full path, complete with the `.xml` extension. Add `var all = assembly.GetManifestResourceNames();`, and print the array `all` line by line. Find the name of your config resource, and copy it into the string `resourcePath`. For the output path, you should use something like `@"c:\appPath\configcopy.xml"` – Sergey Kalinichenko Jan 10 '12 at 04:36
  • Isn't possible using File.Copy? I don't understand how to use the Reflection :/ – Derezzed Jan 10 '12 at 05:04
  • @Derezzed But I though you wanted to copy data from a resource inside your assembly into a file, no? I added code to print the resources from the assembly, could you run the program and tell me what you see? – Sergey Kalinichenko Jan 10 '12 at 05:13
  • NVM, I do it using a webclient to download the file, thanks anyway, I will read more about Reflection component :). Thanks. – Derezzed Jan 10 '12 at 05:22
  • dasblinkenlight , I know this is an old post , just wanted to let you know this helped me and I appreciate your answer ...thanks! – Kevin Moore Jan 04 '18 at 18:19