By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,424 Members | 1,351 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,424 IT Pros & Developers. It's quick & easy.

what is wrong the following code?

P: n/a
I have following code, if you cut and paste it in VS and compile it
you see only an answer of 0. I was expecting values from o through
2999

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Resources;
using System.IO;
using System.Collections;

namespace ResourceTester
{
class Program
{
static void Main(string[] args)
{
//File.Delete("my.resources");
//FileStream fs = new FileStream("my.resources",
FileMode.CreateNew, FileAccess.Write, FileShare.Write, 1,
FileOptions.None);
//ResourceWriter rw = new ResourceWriter(fs);
//for (int i = 1; i < int.MaxValue; i++)
//{
// Console.WriteLine(i.ToString());
// rw.AddResource(i.ToString(),
"XXXXXXXXXXXXXXXXXXXXXXXXXXX" + i.ToString());
//}
//rw.Generate();
//rw.Close();
//fs.Dispose();
//fs.Close();

FileStream fs = new FileStream("my.resources",
FileMode.Create, FileAccess.Write);
IResourceWriter rw = new ResourceWriter(fs);

for(int i=0;i<3000;i++)
{
rw.AddResource(i.ToString(),
"VVVVVVVVVVVVVVVVVVVVVVVVVVV " + i.ToString());

if (i % 1000 == 0)
{
//generate and free resource writer<br>
rw.Generate();
rw.Close();

//free stream<br>
fs.Dispose();
fs.Close();

//reload file<br>
fs = new FileStream("my.resources",
FileMode.Append, FileAccess.Write);
rw = new ResourceWriter(fs);
}
}
//generate and free resource writer<br>
rw.Generate();
rw.Close();

//free stream<br>
fs.Dispose();
fs.Close();

FileStream fsa = new FileStream("my.resources",
FileMode.Open, FileAccess.Read);
IResourceReader rd = new ResourceReader(fsa);
IDictionaryEnumerator dict = rd.GetEnumerator();
while (dict.MoveNext())
{
Console.WriteLine(dict.Key);
}
}
}
}

Any help would be appreciated.

What I am trying to achive is to create a resource file of 3000
numbers, well I could very well directly write the data to the
resource, in reality I want to write all the way up to int.MaxValue.
But when I write, I run into 'Out of Memory' exception. So based on
the suggestion I was told to close and append the file stream, when I
do that at the end I can see the file size is increasing but when you
read the data, only the first file flush data is available.

Thanks.
Jun 28 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
CSharper wrote:
I have following code, if you cut and paste it in VS and compile it
you see only an answer of 0. I was expecting values from o through
2999
<snip>
What I am trying to achive is to create a resource file of 3000
numbers, well I could very well directly write the data to the
resource, in reality I want to write all the way up to int.MaxValue.
But when I write, I run into 'Out of Memory' exception. So based on
the suggestion I was told to close and append the file stream, when I
do that at the end I can see the file size is increasing but when you
read the data, only the first file flush data is available.
I'm speculating here, but it's probably because the resource file has its
own "end of resources" marker embedded, so any data you append is simply
ignored. The only way to achieve this is to write the resources file in one
go, and you apparently can't do that because of the memory restriction.

Conceivably you could try to reverse-engineer or look up the format to
figure out a way to append the data without hitting this issue, but a far
more productive course of action would be to ask why on earth you want to
write 2,147,483,647 resource entries to a file. There's no problem with
having individual resources that are big, but how would you expect to make
effective use of that many different entries?

There is no requirement to use resource files to store data. If you don't
need it to be localizable, you're better off using your own files for very
particular data needs.

--
J.
Jun 28 '08 #2

P: n/a
On Jun 28, 11:40*am, Jeroen Mostert <jmost...@xs4all.nlwrote:
CSharper wrote:
I have following code, if you cut and paste it in VS and compile it
you see only an answer of 0. I was expecting values from o through
2999

<snip>
What I am trying to achive is to create a resource file of 3000
numbers, well I could very well directly write the data to the
resource, in reality I want to write all the way up to int.MaxValue.
But when I write, I run into 'Out of Memory' exception. So based on
the suggestion I was told to close and append the file stream, when I
do that at the end I can see the file size is increasing but when you
read the data, only the first file flush data is available.

I'm speculating here, but it's probably because the resource file has its
own "end of resources" marker embedded, so any data you append is simply
ignored. The only way to achieve this is to write the resources file in one
go, and you apparently can't do that because of the memory restriction.

Conceivably you could try to reverse-engineer or look up the format to
figure out a way to append the data without hitting this issue, but a far
more productive course of action would be to ask why on earth you want to
write 2,147,483,647 resource entries to a file. There's no problem with
having individual resources that are big, but how would you expect to make
effective use of that many different entries?

There is no requirement to use resource files to store data. If you don't
need it to be localizable, you're better off using your own files for very
particular data needs.

--
J.
thank you one reason I want to use the resource to use ILMERGE to
combine all the program, dll and resource files (required informaation
for the program inside the resource file) into one exe file. so I have
two questions
what can I use alternative to resource file to use as a resource in
ilmerge
or does anyone know what is the format or resource file so that I can
manually add?

Thanks.
Jun 28 '08 #3

P: n/a
CSharper wrote:
one reason I want to use the resource to use ILMERGE to
combine all the program, dll and resource files (required informaation
for the program inside the resource file) into one exe file.
I'm not familiar with ILMerge so I couldn't tell you how to use it in this
particular scenario, but I will say that you can compile resources into
separate satellite assemblies, which you could then conceivably merge with
ILMerge. See http://msdn.microsoft.com/library/21a15yht for more information
on that. Whether the program would still be able to find the resources that
are merged this way is another matter.
so I have two questions what can I use alternative to resource file to
use as a resource in ilmerge or does anyone know what is the format or
resource file so that I can manually add?
I wouldn't try manipulating .resources files manually; there's a reason the
framework offers a separate class for reading and writing them.

If you need more control over resource generation, you should use the .resx
format instead, since that's XML. You can compile .resx files into
..resources for embedding into assemblies with resgen.

--
J.
Jun 28 '08 #4

P: n/a
On Jun 28, 6:00*pm, Jeroen Mostert <jmost...@xs4all.nlwrote:
CSharper wrote:
one reason I want to use the resource to use ILMERGE to
combine all the program, dll and resource files (required informaation
for the program inside the resource file) into one exe file.

I'm not familiar with ILMerge so I couldn't tell you how to use it in this
particular scenario, but I will say that you can compile resources into
separate satellite assemblies, which you could then conceivably merge with
ILMerge. Seehttp://msdn.microsoft.com/library/21a15yhtfor more information
on that. Whether the program would still be able to find the resources that
are merged this way is another matter.
so I have two questions what can I use alternative to resource file to
use as a resource in ilmerge or does anyone know what is the format or
resource file so that I can manually add?

I wouldn't try manipulating .resources files manually; there's a reason the
framework offers a separate class for reading and writing them.

If you need more control over resource generation, you should use the .resx
format instead, since that's XML. You can compile .resx files into
.resources for embedding into assemblies with resgen.

--
J.
resx format, do you think, I will able to append data to it? I can't
find the constructor for it in C# 3.0, I thought it was
ResXResourceWriter???
Jun 29 '08 #5

P: n/a
DBC User wrote:
resx format, do you think, I will able to append data to it?
It's XML, so you can't just append at the end -- but since XML is an open
format, you can rewrite these files in any number of ways that don't run
into memory problems. You can't do the same for .resources files because you
don't know the format.
I can't find the constructor for it in C# 3.0, I thought it was
ResXResourceWriter???
It's in System.Windows.Forms, but the class will probably not be of much use
to you, since it cannot be used to append data any more than ResourceWriter
can. What you can do is stream the XML data in and write it out to a new
file until you hit the end, then write out your new resources. This requires
no additional memory.

Please note that if all you want to do is merge resource files and you don't
actually have a memory problem (only the pathological one you created
yourself by going for MaxInt entries) then the problem is much more cleanly
solved by using ResourceWriter or ResXResourceWriter. Read the resources
from one file, read the resources from the second file, add the resources
from the second file to the resources of the first file, write out the new
resource file. It seems unlikely to me that you'd run into memory problems
unless the resource files were already very large to begin with, but if
that's the case then the merged resource file would probably not be loadable.

--
J.
Jun 29 '08 #6

P: n/a
On Jun 29, 3:16*am, Jeroen Mostert <jmost...@xs4all.nlwrote:
DBC User wrote:
resx format, do you think, I will able to append data to it?

It's XML, so you can't just append at the end -- but since XML is an open
format, you can rewrite these files in any number of ways that don't run
into memory problems. You can't do the same for .resources files because you
don't know the format.
I can't find the constructor for it in C# 3.0, I thought it was
ResXResourceWriter???

It's in System.Windows.Forms, but the class will probably not be of much use
to you, since it cannot be used to append data any more than ResourceWriter
can. What you can do is stream the XML data in and write it out to a new
file until you hit the end, then write out your new resources. This requires
no additional memory.

Please note that if all you want to do is merge resource files and you don't
actually have a memory problem (only the pathological one you created
yourself by going for MaxInt entries) then the problem is much more cleanly
solved by using ResourceWriter or ResXResourceWriter. Read the resources
from one file, read the resources from the second file, add the resources
from the second file to the resources of the first file, write out the new
resource file. It seems unlikely to me that you'd run into memory problems
unless the resource files were already very large to begin with, but if
that's the case then the merged resource file would probably not be loadable.

--
J.
Thanks, I went back to resources and instead of creating single
resource files, I have created multiple resource files and during the
lookup, if I don't find it in one resource I switch to another
resource and it works with one caviat. I ran csc to build the project
with all these resources and it compiled and created the exe file. At
the end, I ran ILMerge to build one exe file with other dls I use in
the project, ILMerge fails with Out Of Memory. Now I need to find out,
if there is an alternate way to include the depened dlls in a single
exe using csc/al.exe
Jun 30 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.