Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hey,

I am trying to copy files stored as resources in my wpf project into a new directory.

This is to create a template set of folders with the default files populating it in the directory of the users choice. I have managed to do this with some of the easier file types like icons and plain text but am struggling with finding a way to copy others.

Is there a best way to make a copy of them other than stream reader and writer? this seemes to be handing back empty files for some files?

C#
System.Drawing.Icon icon = Properties.Resources.icons;
              Stream IconStream = System.IO.File.OpenWrite(path+"\\Icons\\test.ico");
              icon.Save(IconStream);

This works fine for an icon but the below code does not work for one of my help files; it creates it but when i look at the file it seems to be empty and wont open
C#
  writer = new StreamWriter(path + "\\Help\\en\\test.chm");
writer.Write(Properties.Resources.test);

(resources.test being a .chm file)

thanks!
Posted
Updated 10-May-20 10:32am
Comments
efkah 7-Aug-12 7:15am    
if during installation then try WIX (to make a package. i guess it wont help you if want them to stay in your resx)

1 solution

think there was a little misunderstanding in my last answer. here is code i actually tested and which should work.

C#
string[] fileNames = Assembly.GetExecutingAssembly().GetManifestResourceNames();
foreach (String fileName in fileNames)
{
    using (FileStream fileStream = File.Create(@"c:\temp\" + fileName))
    {
Assembly.GetExecutingAssembly().GetManifestResourceStream(fileName).CopyTo(fileStream);
    }
}


note that the resource must be marked as "embedded resources" for this to work (right click them in solution explorer, build action "embedded resources").
Maybe not 100% solved, im sorry.
 
Share this answer
 
Comments
Member 14827833 10-May-20 16:34pm    
i copy this code and i make my files to embedded resources
but it this error
'Could not find a part of the path 'c:\temp\WindowsFormsApp3.Form1.resources'.'
Dave Kreskowiak 11-May-20 0:24am    
You might want to actually look at the code you copied and THINK about what the code is doing.

Is your projects running from the "C:\Temp" folder? I'd be willing to bet that the folder doesn't even exist on your machine.

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