c# - Programmatically Load Embedded Resource File -


i implemented following code in order programatically build project/exe. in exe build, wanted store bunch of "actual files" inside of resources, streams.

here's how i'm adding files resource file (singular), , embedding resource file compiler parameters:

        list<string> userfiles = new list<string>();         userfiles.addrange(helpers.getfilesinfolder(this.txt_publish_folder.text));          string folder = this.txt_package_location.text;         folder = folder + "\\package_" + datetime.now.tolongtimestring().replace(":", "_").replace(".", "_");         directory.createdirectory(folder);          compilerparameters parameters = new compilerparameters();         parameters.generateexecutable = true;         parameters.includedebuginformation = true;         parameters.generateinmemory = false;         parameters.warninglevel = 3;         parameters.compileroptions = "/optimize";         parameters.outputassembly = folder + "\\install_" + this.txt_appname.text + ".exe";         parameters.referencedassemblies.add("microsoft.csharp.dll");         parameters.referencedassemblies.add("system.dll");         parameters.referencedassemblies.add("system.core.dll");         parameters.referencedassemblies.add("system.data.dll");         parameters.referencedassemblies.add("system.data.datasetextensions.dll");         parameters.referencedassemblies.add("system.xml.dll");         parameters.referencedassemblies.add("system.xml.linq.dll");          codedomprovider codeprovider = codedomprovider.createprovider("csharp");         if (codeprovider.supports(generatorsupport.resources))         {             string temp = path.combine(path.gettemppath(), path.gettempfilename());             //create temp file first, because want append have single resource file multiple stream entries...             file.writealltext(temp, null);             (int = 0; < userfiles.count; i++ )             {                 byte[] filebytes = file.readallbytes(userfiles[i]);                  using (filestream stream = new filestream(temp, filemode.append))                 {                     using (resourcewriter writer = new resourcewriter(stream))                     {                         writer.addresource(path.getfilename(userfiles[i]), filebytes);                     }                 }             }             parameters.embeddedresources.add(temp);         }          compilerresults res = codeprovider.compileassemblyfromfile(parameters, @"c:\hiddenpath\program.cs"); 

this works great, doesn't yield errors, , can embedded resource file out in console application built (the 1 referenced above prorgam.cs) through code below. problem having "loading" resource file application assembly / getting values out somehow... here's code have far that:

    static void main(string[] args)     {         console.writeline("checking resources... please wait...");          assembly thisexe;         thisexe = assembly.getexecutingassembly();         list<string> resources = thisexe.getmanifestresourcenames().tolist();          if(resources.count >= 1)         {             try             {                 string basename = resources[0];                 resourcemanager mgr = new resourcemanager(basename, thisexe);                 console.writeline("retrieved manager...");                 console.readline();                 resourceset set = mgr.getresourceset(thread.currentthread.currentculture, true, true);                 int count = set.cast<object>().count();                 console.writeline("found [" + count.tostring() + "] embedded resources. enumerate them?");                 consolekeyinfo input = console.readkey();                 if (input.key == consolekey.y)                 {                     // build string of resources.                     foreach (string resource in resources)                         console.writeline(resource);                 }              }             catch (exception ex)             {                 console.write(ex.message);                 console.readline();             }         }          console.readline();     } 

whenever run built exe, following result:

  checking resources... please wait... retrieved manager...  not find resources appropriate specified culture or neutral culture.  make sure "tmpcc59.tmp.resources" correctly embedded or linked assembly "install_testing" @ compile time, or satellite assemblies required loadable , signed.  

can please tell me why happenning? i've tried different culture sets think of , still nothing comes out. problem related how i'm "embedding" or how i'm "loading" ?

ok folks, ended with, fixed problems.

  1. instead of trying add "resource entries" single "resource file", i'm adding every file own resource, directly source file (don't know why thought making temp copy of original , using stream necessary it's not) this:

            (int = 0; < webappfiles.count; i++)         {             parameters.embeddedresources.add(webappfiles[i]);         } 
  2. then, when comes time extract actual files, instead of trying load "set" single "resource file", extract data each embedded resource, this:

                    (int = 0; < resources.count; i++)                 {                     console.writeline("extracting file: " + resources[i] + "...");                     stream stream = thisexe.getmanifestresourcestream(resources[i]);                     byte[] bytes = new byte[(int)stream.length];                     stream.read(bytes, 0, bytes.length);                     file.writeallbytes(dir + resources[i], bytes);                 } 

this new way means tell compiler "these files necessary exe , embedded", when call "build" command, work , reads files in byte arrays , embeds executable file.

then, @ other hand, tell program want save embedded resources files, using own file names.

hopefully helps else trying this... every other post type of question had on complicated answers either incomplete (e.g.: how embed, not retrieve) or didn't work.


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -