Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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)
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:
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:
richTextBox.Text = file_name;
Still no luck.
 
Does anybody know what I'm doing wrong, help me!
Thanks.
Posted 31-Dec-12 1:20am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
v2
Comments
DarkTyranno at 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 at 31-Dec-12 8:23am
   
See update
DarkTyranno at 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 at 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 at 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 ;)
Alan N at 31-Dec-12 9:47am
   
You mentioned several options in your original post for accessing the resource. What is the actual code you are using to put the resource text into the RichTextBox?
 
Edit the original post to make this clear.
DarkTyranno at 31-Dec-12 10:06am
   
I may have the solution, as you said to change the encoding in the resource file itself, I don't need to read the file again (String doesn't support .ReadToEnd()?) and encode it again. I've tried what I did before:
 
string file_name = Properties.Resources.DARTY;
richTextBox1.Text = file_name;
 
and got rid of:
System.Text.Encoding encoding = System.Text.Encoding.GetEncoding(437);
System.IO.StreamReader file = new System.IO.StreamReader(file_name, encoding);
richTextBox1.Text = file.ReadToEnd();
file.Close();
 
Now suddenly the text appears! :D
 
So as a summary:
Add the nfo-file to resources using the Resources tab of Project Properties;
View detailed mode and change the FileType and Encoding of the added Resource nfo-file.
Call it using Properties.Resources.FileName;
 
No need to convert or read. Thanks for the steps that I needed to get this working, now I can go figure how to add .it music to resources as well ;)
Alan N at 31-Dec-12 10:48am
   
Good, now I understand what confused you. Although VS refers to the file within the resources, it actually only imports it's contents and you don't have to open and read the file.

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

  Print Answers RSS
0 OriginalGriff 277
1 Maciej Los 210
2 BillWoodruff 205
3 Jochen Arndt 180
4 DamithSL 165
0 OriginalGriff 5,130
1 DamithSL 4,157
2 Maciej Los 3,670
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,821


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 31 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100