resources in vs.net

M

Marcus Ahlberg

I've been coding a C# project in "pure code" and I would now want to import
it into VS.net. I have created a project and imported all sourcefiles and
it's compiling without problems, except for the resources. Before, I built
the program using a batchfile and linked the resources with the
"/resource:MainFrame.resX" option. Then I loaded them with this code:

Assembly myAssembly = Assembly.GetAssembly(this.GetType());
Stream resXStream =
myAssembly.GetManifestResourceStream("MainFrame.resX");
ResXResourceSet resX = new ResXResourceSet(resXStream);

The problem is the way VS manages resources. It can't find MainFrame.resX
and i get an error because resXStream = null. I've tried to change the
"Build Action" -property of the files, but it doesn't seem to make any
difference. It's the same problem with images. Is there any way to change
the way the resources is included? I don't want to rewrite the whole program
because of incompability between VS and the compiler.
 
M

Marcus Ahlberg

I've added the files to the project and tried different settings for Build
Action. I won't be using the WinForm-designer since my forms are handmade. I
just want to edit my code in vs and use the build function.
This problem applies to all my images too, and I just want my resources to
be included the same way that the "/resource:image.png" option does.
Is there any way to send this option to the compiler manualy in VS?
Thanks for trying to help me!
 
J

Jay B. Harlow [MVP - Outlook]

Marcus,
That's fine, you do not need to use the WinForm Designer with VS.NET.

My question still stands: Did you add the MainFrame.resx to your project,
and set the build action to Embedded Resource.

As it sounds like you want to embed the resource in the exactable, to do
this with VS.NET, you add the resx file to your project, and set the build
action to embedded resource. Did you do that?

If you have done the above, and it is still not working, I suspect the
namespace of your resource does not match the namespace you are trying to
use to retrieve it. You can use Reflector to verify the namespace. See
http://www.aisto.com/roeder/dotnet/ for Reflector.
Is there any way to send this option to the compiler manually in VS?
Yes there is! ;-)

Build Action = Embedded Resource. Works for .resx files and any other file
type you give it.

Hope this helps
Jay
 
M

Marcus Ahlberg

Yes I set the Build action to Embeded Resource, but it is not exactly the
same thing.

For the images, it seems like they are included the same way, but they are
all put in a namespace like "Marcusoft.TextWrite.res.CommandIcons.bmp" but I
want them to exist outside the namespace like "CommandIcons.bmp". Do you
know how to specify witch namespace to put them in?

I called "myAssembly.GetManifestResourceNames()" and I found the name
"Marcusoft.TextWrite.res.MainFrame.resources", but it is not a valid resX
input. It seems like VS is doing something with the resX-files.
 
J

Jay B. Harlow [MVP - Outlook]

Marcus,
Yes I set the Build action to Embeded Resource, but it is not exactly the
same thing.
For bitmaps, other than namespace how is it different? As your found out,
..resx files are actually compiled and embedded as .resources.
Do you know how to specify witch namespace to put them in?
I do not know how to control the namespace that VS.NET uses when it includes
the resource. I wish I did.

The little bit of testing I have done with it, C# seems to behave better
about it, then VB.NET does. In that the namespaces that C# uses seem more
natural.
I called "myAssembly.GetManifestResourceNames()" and I found the name
"Marcusoft.TextWrite.res.MainFrame.resources", but it is not a valid resX
input. It seems like VS is doing something with the resX-files.
VS.NET compiles your .resx files into .resource files, it then embeds the
..resource file into the assembly. You will need to change your code that
uses .resx to use .resource. The ResourceManager makes this rather easy.

http://msdn.microsoft.com/library/d...ry/en-us/cptutorials/html/resourcemanager.asp

Hope this helps
Jay

Marcus Ahlberg said:
Yes I set the Build action to Embeded Resource, but it is not exactly the
same thing.

For the images, it seems like they are included the same way, but they are
all put in a namespace like "Marcusoft.TextWrite.res.CommandIcons.bmp" but I
want them to exist outside the namespace like "CommandIcons.bmp". Do you
know how to specify witch namespace to put them in?

I called "myAssembly.GetManifestResourceNames()" and I found the name
"Marcusoft.TextWrite.res.MainFrame.resources", but it is not a valid resX
input. It seems like VS is doing something with the resX-files.

Jay B. Harlow said:
Marcus,
That's fine, you do not need to use the WinForm Designer with VS.NET.

My question still stands: Did you add the MainFrame.resx to your project,
and set the build action to Embedded Resource.

As it sounds like you want to embed the resource in the exactable, to do
this with VS.NET, you add the resx file to your project, and set the build
action to embedded resource. Did you do that?

If you have done the above, and it is still not working, I suspect the
namespace of your resource does not match the namespace you are trying to
use to retrieve it. You can use Reflector to verify the namespace. See
http://www.aisto.com/roeder/dotnet/ for Reflector.

Yes there is! ;-)

Build Action = Embedded Resource. Works for .resx files and any other file
type you give it.

Hope this helps
Jay

handmade.
resources
to
be included the same way that the "/resource:image.png" option does.
Is there any way to send this option to the compiler manualy in VS?
Thanks for trying to help me!

"Jay B. Harlow [MVP - Outlook]" <[email protected]> skrev i
meddelandet Marcus,
Did you include MainFrame.ResX in your project?

Did you set the Build Action (in the properties window) for the
above
file
to Embedded Resource?

Watch namespaces. Use Reflector to verify the names of the resources in
the
assembly.

Note: If you have a form names MainFrame, VS.NET will create a
MainFrame.Resx for you. Use Show All Files in solution explorer.

Hope this helps
Jay

I've been coding a C# project in "pure code" and I would now want to
import
it into VS.net. I have created a project and imported all sourcefiles
and
it's compiling without problems, except for the resources. Before, I
built
the program using a batchfile and linked the resources with the
"/resource:MainFrame.resX" option. Then I loaded them with this code:

Assembly myAssembly = Assembly.GetAssembly(this.GetType());
Stream resXStream =
myAssembly.GetManifestResourceStream("MainFrame.resX");
ResXResourceSet resX = new ResXResourceSet(resXStream);

The problem is the way VS manages resources. It can't find
MainFrame.resX
and i get an error because resXStream = null. I've tried to change the
"Build Action" -property of the files, but it doesn't seem to make any
difference. It's the same problem with images. Is there any way to
change
the way the resources is included? I don't want to rewrite the whole
program
because of incompability between VS and the compiler.
 
M

Marcus Ahlberg

It seems like the only way is to rewrite my code.
Thanks for your help.

Jay B. Harlow said:
Marcus,
Yes I set the Build action to Embeded Resource, but it is not exactly the
same thing.
For bitmaps, other than namespace how is it different? As your found out,
.resx files are actually compiled and embedded as .resources.
Do you know how to specify witch namespace to put them in?
I do not know how to control the namespace that VS.NET uses when it includes
the resource. I wish I did.

The little bit of testing I have done with it, C# seems to behave better
about it, then VB.NET does. In that the namespaces that C# uses seem more
natural.
I called "myAssembly.GetManifestResourceNames()" and I found the name
"Marcusoft.TextWrite.res.MainFrame.resources", but it is not a valid resX
input. It seems like VS is doing something with the resX-files.
VS.NET compiles your .resx files into .resource files, it then embeds the
.resource file into the assembly. You will need to change your code that
uses .resx to use .resource. The ResourceManager makes this rather easy.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cptutorials
/html/resourcemanager.asp

Hope this helps
Jay

Marcus Ahlberg said:
Yes I set the Build action to Embeded Resource, but it is not exactly the
same thing.

For the images, it seems like they are included the same way, but they are
all put in a namespace like "Marcusoft.TextWrite.res.CommandIcons.bmp"
but
I
want them to exist outside the namespace like "CommandIcons.bmp". Do you
know how to specify witch namespace to put them in?

I called "myAssembly.GetManifestResourceNames()" and I found the name
"Marcusoft.TextWrite.res.MainFrame.resources", but it is not a valid resX
input. It seems like VS is doing something with the resX-files.

Jay B. Harlow said:
Marcus,
That's fine, you do not need to use the WinForm Designer with VS.NET.

My question still stands: Did you add the MainFrame.resx to your project,
and set the build action to Embedded Resource.

As it sounds like you want to embed the resource in the exactable, to do
this with VS.NET, you add the resx file to your project, and set the build
action to embedded resource. Did you do that?

If you have done the above, and it is still not working, I suspect the
namespace of your resource does not match the namespace you are trying to
use to retrieve it. You can use Reflector to verify the namespace. See
http://www.aisto.com/roeder/dotnet/ for Reflector.

Is there any way to send this option to the compiler manually in VS?
Yes there is! ;-)

Build Action = Embedded Resource. Works for .resx files and any other file
type you give it.

Hope this helps
Jay

I've added the files to the project and tried different settings for Build
Action. I won't be using the WinForm-designer since my forms are handmade.
I
just want to edit my code in vs and use the build function.
This problem applies to all my images too, and I just want my
resources
to
be included the same way that the "/resource:image.png" option does.
Is there any way to send this option to the compiler manualy in VS?
Thanks for trying to help me!

"Jay B. Harlow [MVP - Outlook]" <[email protected]> skrev i
meddelandet Marcus,
Did you include MainFrame.ResX in your project?

Did you set the Build Action (in the properties window) for the above
file
to Embedded Resource?

Watch namespaces. Use Reflector to verify the names of the
resources
in
the
assembly.

Note: If you have a form names MainFrame, VS.NET will create a
MainFrame.Resx for you. Use Show All Files in solution explorer.

Hope this helps
Jay

I've been coding a C# project in "pure code" and I would now
want
Before,
I change
the make
any
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top