Solved

Write from Resource to disk using c#

Posted on 2014-01-13
3
27 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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…
This is about my first experience with programming Arduino.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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 …

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now