Solved

Write from Resource to disk using c#

Posted on 2014-01-13
3
35 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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 …
Introduction to Processes

630 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