0

The idea here is to extract a batch file from an embedded resource to a temporary directory on the C drive, then run that extracted batch file. The issue I'm running into is an error of "Object reference not set to an instance of an object."

The code is:

public static void ExtractResource()
    {
        using (Stream s = Assembly.GetExecutingAssembly().GetManifestResourceStream(Namespace.Properties.Resources.Backup))
        {
            byte[] buffer = new byte[s.Length];
            s.Read(buffer, 0, buffer.Length);
            using (var sw = new BinaryWriter(File.Open(@"C:\test.bat", FileMode.OpenOrCreate)))
            {
                sw.Write(buffer);
            }
        }
    }

The error occurs on this line:

byte[] buffer = new byte[s.Length];
2
  • According to the doco. The manifest resource; or null if no resources were specified during compilation or if the resource is not visible to the caller. Commented Jul 19, 2013 at 5:52
  • @darkstarohio what is the feedback on these suggestions ? Commented Jul 28, 2013 at 13:31

3 Answers 3

1

if you embedded resource then it will generate static method to to get content of embedded file content, internally it call the ResourceManager.GetString method, so try below

using (var sw = new BinaryWriter(File.Open(@"C:\test.bat", FileMode.OpenOrCreate)))
{
   writer.Write(Namespace.Properties.Resources.Backup);
}
Sign up to request clarification or add additional context in comments.

Comments

0

It sounds like the stream doesn't get returned or it isn't reporting length.

Also, don't read the whole stream at once, it's not very memory efficient and isn't the best pattern to follow when you start using network streams or large files which may not actually report their actual length.

public void WriteResrouce(string resourcePath, string targetPath)
{
    var buffer = new byte[64 * 1024]; //i've picked 64k as a reasonable sized block
    using (Stream s = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourcePath))
    using (var sw = new BinaryWriter(File.Open(targetPath, FileMode.OpenOrCreate)))
    {
        var readCount = -1;
        while (0 < (readCount = s.Read(buffer, 0, buffer.Length)))
        {
            sw.Write(buffer, 0, readCount);
        }
    }
}

Comments

0

How is the following working for you?

    public static  class Program
        {
            public static void Main()
            {
                ExtractResource();
            }

            public static void ExtractResource()
            {
                //replace your embedded file by yours 
                using (var inputStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("ConsoleApplication1.XMLFile1.xml"))
                {
                    using(var outputStream = File.Create(@"D:\test.bat"))
                    {
                        /// fix your buffer size 8192,4096 etc.
                        var buffer = new byte[8192];

                        int numberOfBytesRead;
                        while (inputStream != null && (numberOfBytesRead = inputStream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            outputStream.Write(buffer, 0, numberOfBytesread);
                        }
                    }
                }

            }
       }

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.