Solved

Write from Resource to disk using c#

Posted on 2014-01-13
3
31 Views
Last Modified: 2016-06-13
I have 9 (will be more) files that are embedded resources. They will all be different file types. I'm trying to figure out a way to loop thru them, and write them to the hard drive. I've yet to find an example I can get to work. Any and all help appreciated.
0
Comment
Question by:gnixon14
3 Comments
 
LVL 12

Expert Comment

by:HugoHiasl
ID: 39777984
In which way do you have them available? MemoryStreams? Or how can the files be accessed within the code?
0
 

Accepted Solution

by:
gnixon14 earned 0 total points
ID: 39778067
I got this to work.

    private void mRecreateAllExecutableResources()
    {
      // Get Current Assembly refrence
      Assembly currentAssembly = Assembly.GetExecutingAssembly();
      // Get all imbedded resources
      string[] arrResources = currentAssembly.GetManifestResourceNames();

      foreach (string resourceName in arrResources)
      {
        if (resourceName.EndsWith(".xml"))
        { //or other extension desired
          //Name of the file saved on disk
          string saveAsName = resourceName;
          FileInfo fileInfoOutputFile = new FileInfo(@"c:\users\gary\desktop\" + saveAsName);
          //CHECK IF FILE EXISTS AND DO SOMETHING DEPENDING ON YOUR NEEDS
          if (fileInfoOutputFile.Exists)
          {
            txtResults.Text += saveAsName + " Exist" + Environment.NewLine;
            //overwrite if desired  (depending on your needs)
            //fileInfoOutputFile.Delete();
          }
          //OPEN NEWLY CREATING FILE FOR WRITTING
          FileStream streamToOutputFile = fileInfoOutputFile.OpenWrite();

          //GET THE STREAM TO THE RESOURCES
          Stream streamToResourceFile =
                              currentAssembly.GetManifestResourceStream(resourceName);

          //---------------------------------
          //SAVE TO DISK OPERATION
          //---------------------------------
          const int size = 4096;
          byte[] bytes = new byte[4096];
          int numBytes;
          while ((numBytes = streamToResourceFile.Read(bytes, 0, size)) > 0)
          {
            streamToOutputFile.Write(bytes, 0, numBytes);
          }
          txtResults.Text += saveAsName + " Created" + Environment.NewLine;
          streamToOutputFile.Close();
          streamToResourceFile.Close();
        }//end_if
      }//end_foreach
    }//end_mRecreateAllExecutableResources

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about a problem I had getting the GPS LocationListener working.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question