Solved

Write from Resource to disk using c#

Posted on 2014-01-13
3
33 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

734 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