0

I have embedded files in a program of mine previously with full success but I have now transferred the lines of code over to a second program and to my disappointment, I just cant get it to work for the life of me.

The code for the 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 the program I want located in a folder named NewFolder1 I am typing the 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 the program with no errors but once the program reaches the line to extract:

Extract(n, TempFileLoc, "NewFolder1", "Extract1.exe");

The program crashes and I get an error: "value cannot be null"

And YES I included System.IO & System.Reflection

3
  • You should be able to find out what exactly is null using Visual Studio debugger. Run your code with debugging and step by step check that obtained values are not null. Commented Aug 28, 2013 at 11:30
  • The problem is the debugger finds nothing, I only find an issue once I try running the program to extract the EXE. Commented Aug 28, 2013 at 11:34
  • First guess is that s is null. GetManifestResourceStream() doc says: The manifest resource; or null if no resources were specified during compilation or if the resource is not visible to the caller. Commented Aug 28, 2013 at 11:37

1 Answer 1

1

A couple of things.

First, you probably should add some error checking so that you can figure out where things are 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 do the same thing when opening your FileStream.

If you make those changes, you can single-step in the debugger or write code to output trace information that tells you exactly where the error is occurring.

Second, there's no need for the BinaryReader or BinaryWriter here. You can write:

s.CopyTo(fs);

Which will copy the entire stream contents.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.