Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Write from Resource to disk using c#

Posted on 2014-01-13
3
Medium Priority
?
39 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Starting up a Project

963 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