embedded resource - Error extracting a file from a program C# -


i have embedded files in program of mine full success have transferred lines of code on second program , disappointment, cant work life of me.

the code extraction is:

private static void extract(string namespace, string outdirectory, string internalfilepath, string resourcename)     {         assembly assembly = assembly.getcallingassembly();          using (stream s = assembly.getmanifestresourcestream(namespace + "." + (internalfilepath == "" ? "" : internalfilepath + ".") + resourcename))         using (binaryreader r = new binaryreader(s))         using (filestream fs = new filestream(outdirectory + "\\" + resourcename, filemode.openorcreate))         using (binarywriter w = new binarywriter(fs))             w.write(r.readbytes((int)s.length));     } 

to extract program want located in folder named newfolder1 typing code:

type mytype = typeof(newprogram);             var n = mytype.namespace.tostring();             string tempfileloc = system.environment.getenvironmentvariable("temp");             extract(n, tempfileloc, "newfolder1", "extract1.exe"); 

i can compile program no errors once program reaches line extract:

extract(n, tempfileloc, "newfolder1", "extract1.exe"); 

the program crashes , error: "value cannot null"

and yes included system.io & system.reflection

a couple of things.

first, should add error checking can figure out things failing. rather than:

using (stream s = assembly.getmanifestresourcestream(namespace + "." +    (internalfilepath== "" ? "" : internalfilepath + ".") + resourcename)) 

write:

string name = namespace + "." +    (internalfilepath== "" ? "" : internalfilepath + ".") + resourcename; stream s = assembly.getmanifestresourcestream(name); if (s == null) {     throw new applicationexception(); // or whatever }  using (s) {     // other stuff here } 

you should same thing when opening filestream.

if make changes, can single-step in debugger or write code output trace information tells exactly error occurring.

second, there's no need binaryreader or binarywriter here. can write:

s.copyto(fs); 

which copy entire stream contents.


Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -