Chaos Project

Game Development => Sea of Code => Topic started by: ForeverZer0 on February 27, 2011, 02:56:20 am

Title: [C#] Reflection Help
Post by: ForeverZer0 on February 27, 2011, 02:56:20 am
Okay, I have been working on a small application to generate configuration files for my CCTS script.
I have also included a feature for the main part of the script to be generated if the player wants to. I added a a .txt file to the project, which is the main part of the script, set its properties to embed as a resource. I then use this code after the player chooses the save directory:

                
               DialogResult result = dialog.ShowDialog();
               if (result == DialogResult.OK)
               {
                   try
                   {
                       // Load the embedded resource from the assembly
                       Assembly assembly = Assembly.GetEntryAssembly();
                       StreamReader docScreenTest =
                           new StreamReader(assembly.GetManifestResourceStream(
                               "ForeverZer0.RMXP.CCTSConfig.CCTS v.1.2.3 Main.txt"));
                       File.WriteAllText(dialog.FileName, docScreenTest.ReadToEnd(),
                           System.Text.Encoding.Unicode);
                   }
                   catch (Exception error)
                   {
                       MessageBox.Show("Error accessing embedded resource.\r\n\r\n" + error.Message, "Load Error");
                   }


Here is the error message I get that pops up in the catch block:

Quote from: From the damn message box
Error accessing embedded resource.

Value cannot be null.
Parameter name: stream


I counted out all the dumb problems such as wrong filename, wrong namespace, etc., etc.
This has something to do with the difference between .NET 2.0 and .NET 4.0. I had just gotten done converting it from 4.0 to 2.0 for the sake of compatibility, and was debugging when this popped up. It compiles fine, and when I run a code analysis with VS, I don't get a single warning. I tested this completely before the switch and it was working perfectly.

Any ideas?
Title: Re: [C#] Reflection Help
Post by: G_G on February 27, 2011, 03:20:07 am
Get the resource this way.
StreamReader file = new StreamReader(Namespace.Properties.Resources.Resource_Name)

Namespace is your projects namespace. Replace resource name with the resource you're trying to get.
I have blue.png in my resources
Image img = GameGuy.Properties.Resources.blue


Should work.

EDIT: Sorry forgot some code  :^_^':

EDIT: Just did it myself and heres how you do it.
Text file embedded - "Script what not crap" - readme.txt
File.WriteAllText(dialog.FileName, Properties.Resources.readme
                            System.Text.Encoding.Unicode);


When accessing embedded text files, it automatically grabs the text inside it.
Title: Re: [C#] Reflection Help
Post by: ForeverZer0 on February 27, 2011, 03:57:24 am
I can't seem to get that to work.
Using the resource designer, I added a text file, named it "ScriptMain", then tried this:

StreamReader file = 
StreamReader(ForeverZer0.RMXP.CCTSConfig.Properties.Resources.ScriptMain)

This won't even compile since the "Properties" namespace is not defined.

I also tried with brackets " ", but still to no avail. Same when I added the .txt to the end of the name (both file and resource are labeled the same).

Did I do something wrong?
Title: Re: [C#] Reflection Help
Post by: G_G on February 27, 2011, 03:59:56 am
Example. Project set to .NET 2.0 with embedded text file and accessing it on run-time. Also you may have missed my edits. Anyways here.

http://decisive-media.net/gameguy/here.zip
Title: Re: [C#] Reflection Help
Post by: ForeverZer0 on February 27, 2011, 04:02:15 am
Oops. yeah I missed your edits. I never left the page after reading it, then just made another post, so it wasn't refreshed.
Downloading file now... Thanks!  ;)