[Extracting C#] Extracting Resources = OutofMemory

Joined
Sep 25, 2008
Messages
457
Reaction score
37
Here:
Code:
            byte[] clientwz = Properties.Resources.NudieStory;
            File.WriteAllBytes(textBox1.Text + "\\NudieStory.exe", clientwz);

I don't know why you need the code for when the code isn't the problem.

What exactly are you trying to do?
Make an installer? o.o
 
Well I am trying to get big files extracted into a directory. I hit a snag. It's to big so it shows the error 'OutOfMemory'. I heard that making temporary files for the resource files I am trying to get it extracting will fix it and not sure if it will work. How can I extract big files for resources and fix the 'OutOfMemory'?

Don't post saying just put into a .rar and send it, I want it as an installer so I am trying to do it this way.
 
Show the code you're using to extract the resources

Here:
Code:
            byte[] clientwz = Properties.Resources.NudieStory;
            File.WriteAllBytes(textBox1.Text + "\\NudieStory.exe", clientwz);

I don't know why you need the code for when the code isn't the problem.
 
eh... if u are making a installer.. why the hell are u writing a whole file as u go? I am sure its not done this way.. rather u should probably open a System.IO file stream to stream the bytes into the file. Thats how they are able to come up with the progress bar for each file
 
Here:
Code:
            byte[] clientwz = Properties.Resources.NudieStory;
            File.WriteAllBytes(textBox1.Text + "\\NudieStory.exe", clientwz);

I don't know why you need the code for when the code isn't the problem.

Do you dispose your **** when your done?

BTW I wouldn't open an EXE like that, it's suicide generally speaking. I recommend a stream ( Like everyone else already said. )

Streams are a lot safer and a lot easier to use and can easily be ported to little helper classes that microsoft made like "BinaryWriter"

That is, considering that you're writing in C#, if you're not; then best of luck.
 
i don't think using a filestream is the problem here.

is the OOM exception thrown at this line?:
byte[] clientwz = Properties.Resources.NudieStory;

if yes then the problem is getting the resource not how to save it.

maybe try to open it like shown here:
http://support.microsoft.com/kb/319292

Code:
_assembly = Assembly.GetExecutingAssembly();
_imageStream = _assembly.GetManifestResourceStream("MyNamespace.MyImage.bmp");

after that create a binaryreader, filestream and a binarywritter and loop over the resource stream and read/write the data.

something like this:

Code:
_assembly = Assembly.GetExecutingAssembly();
resStream = new BinaryReader(_assembly.GetManifestResourceStream("MyNamespace.MyImage.bmp"));


string fileName = myfolder.Text + "\\awesome.exe";

byte[] buffer = new byte[1024];
using(BinaryWriter binWriter = new BinaryWriter(File.Open(fileName, FileMode.Create)))
{
	while (resStream.BaseStream.Position < resStream.BaseStream.Length)
	{
		int cnt = resStream.Read(buffer, 0, 1024); // cnt may be less than 1024 when the stream is at the end
		binWriter.Write(buffer, 0, cnt);
	}
}

resStream.Close();
 
Last edited:
i don't think using a filestream is the problem here.

is the OOM exception thrown at this line?:
byte[] clientwz = Properties.Resources.NudieStory;

if yes then the problem is getting the resource not how to save it.

maybe try to open it like shown here:
http://support.microsoft.com/kb/319292

Code:
_assembly = Assembly.GetExecutingAssembly();
_imageStream = _assembly.GetManifestResourceStream("MyNamespace.MyImage.bmp");

after that create a binaryreader, filestream and a binarywritter and loop over the resource stream and read/write the data.

something like this:

Code:
_assembly = Assembly.GetExecutingAssembly();
resStream = new BinaryReader(_assembly.GetManifestResourceStream("MyNamespace.MyImage.bmp"));


string fileName = myfolder.Text + "\\awesome.exe";

byte[] buffer = new byte[1024];
using(BinaryWriter binWriter = new BinaryWriter(File.Open(fileName, FileMode.Create)))
{
	while (resStream.BaseStream.Position < resStream.BaseStream.Length)
	{
		int cnt = resStream.Read(buffer, 0, 1024); // cnt may be less than 1024 when the stream is at the end
		binWriter.Write(buffer, 0, cnt);
	}
}

resStream.Close();

It saves with the resource way but it just gives 'OutOfMemory'.

FileStream, I tried but I don't know how to do it. I tried maybe 6 times.
 
what gives you the OOm exception?

Also my example already has a FileStream example in it.

using(BinaryWriter binWriter = new BinaryWriter(File.Open(fileName, FileMode.Create)))
File.Open() returns a FireStream object, the binary writter constructor takes it as an argument and then i use binWriter.Write() to write to that stream. At the end of the using(){} block it closes the stream and force-flushes everything into the file.
 
Back