Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey all, I've got a small program to show a .nfo text file in a texbox (refer to my project here: Creating-a-NFO-Viewer-in-Csharp-as-a-beginner[^])

The problem at this point is that I have to sent the nfo file with my program each time! This also means I have to select the path to read the nfo from on the target client... There must be some way to use this file as resource, so it can open the file from within the .exe? Can someone help me with that?

The code that's in the spotlight for this part is this:
(on form_load)
C#
string file_name = "C:\\Test\\DARTY.nfo"; // load from computer...

if (System.IO.File.Exists(file_name) == true) // check if there.
   {
      System.Text.Encoding encoding = System.Text.Encoding.GetEncoding(437); // select text to nfo.
      System.IO.StreamReader file = new System.IO.StreamReader(file_name, encoding); // use converter for our file.
      richTextBox1.Text = file.ReadToEnd(); // set text in box to converted file.
      file.Close();
   }
   else // file not found or doesn't exist.
   {
      richTextBox1.Text = "File not Found! :( --- Where is DARTY.nfo??"; // set text.
   }

How can I make this nfo file a resource?
I've tried richtclick project -> properties -> resources. Add resource (String or File).
Then in the code I tried the following, set:
C#
string file_name = Properties.Resources.DARTY;

And edit the rest of the code accordingly.
The problem I got was: Error, cannot convert byte[] to string.
Well, I could change byte[] in the resources.designer.cs to string.
Then all seems okay, no errors. But when I run my program, no text appears in the box. I also tried disabling all converting, just directly:
C#
richTextBox.Text = file_name;

Still no luck.

Does anybody know what I'm doing wrong, help me!
Thanks.
Posted

1 solution

If Visual Studio recognises the file extension of an imported resource it sets the resource type. For example .ico is assumed to contain an icon and .txt is assumed to contain string data. For all unrecognised extensions, which includes .nfo, the resource type is set to array of byte.

It easy to change the default assumptions in the resource properties. Initially the FileType will be Binary and this should be changed to Text by selecting from the dropdown. The Encoding property will become active where you should select Codepage 437.

The resource accessor Properties.Resources.XXXXXXXX should now give a correctly encoded string (fingers crossed, touch wood).

[EDIT]
To find the resource properties in VS
1) Goto project properties (Menu..Project..Properties)
2) Select the resources page
3) Set the page to display the Files (I usually set it to details view as well)
4) Make the properties window visible (Menu..View..Properties Window)
5) Select a resource file to update the properties window
That should be it, but it's difficult to give more precise instructions as VS layouts can be customised.

Alan.
 
Share this answer
 
v2
Comments
DarkTyranno 31-Dec-12 7:47am    
I don't exactly get what I need to do...
Where do I have to change setting Binary to Text? Which dropdown?
Alan N 31-Dec-12 8:23am    
See update
DarkTyranno 31-Dec-12 8:34am    
That step did work to change that value, it seems you don't get all options when you select the properties of the file in the Resource folder. I had thumbnail view, but detailed view gave more properties. I changed to Text and the encoding to 437.
Now I still have the problem of no file showing up when I try to call the resource... Any idea?
Alan N 31-Dec-12 9:21am    
Difficult to say. I had a look in the source from your article but you don't include the .nfo file. For me that would be the start of any debugging effort. Are the file contents what it should be?

As a quick test I've changed the extension of a text file to .nfo, imported the file as a resource using the technique outlined in my answer and can subsequently read the text correctly via the resource accessor.
DarkTyranno 31-Dec-12 9:38am    
In that situation I still deliver the file with my application, which means users have to put it in specified location and run the program. I want to melt it together with my application, but nothing appears after I've added it in my project as a resource. Hope other people will have a look into this, I'll try some more in the meantime ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900