使用(.NFO)文本文件作为.exe中的资源,如何? [英] Using a (.NFO) text file as resource in .exe, how?

查看:119
本文介绍了使用(.NFO)文本文件作为.exe中的资源,如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿所有,我有一个小程序在texbox中显示.nfo文本文件(请参阅我的项目:创建一个NFO-Viewer-in-Csharp-as-a-beginner [ ^ ])



此时的问题是我每次都必须用我的程序发送nfo文件!这也意味着我必须选择从目标客户端读取nfo的路径...必须有一些方法可以将此文件用作资源,因此它可以从.exe中打开文件?有人可以帮我这个吗?



这个部分备受关注的代码是:

(在form_load上)

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.
   }



如何将这个nfo文件作为资源?

我尝试过richtclick project - > properties - > resources。添加资源(字符串或文件)。

然后在代码中我尝试了以下内容,设置:


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;



并相应地编辑其余代码。

我遇到的问题是:错误,无法将byte []转换为字符串。

好​​吧,我可以将resources.designer.cs中的byte []更改为string。

然后一切似乎没问题,没有错误。但是当我运行我的程序时,框中没有文字出现。我也试过直接禁用所有转换:


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.

推荐答案

如果Visual Studio识别导入资源的文件扩展名,则设置资源类型。例如,假设.ico包含一个图标,并假定.txt包含字符串数据。对于所有无法识别的扩展(包括.nfo),资源类型设置为byte数组。



很容易更改资源属性中的默认假设。最初,FileType将是Binary,通过从下拉列表中选择,应将其更改为Text。编码属性将在您应选择代码页437的位置激活。



资源访问者 Properties.Resources.XXXXXXXX 现在应该给出一个正确编码的字符串(手指交叉,触摸木头)。





在VS中查找资源属性< br $>
1)转到项目属性(Menu..Project..Properties)

2)选择资源页面

3)设置要显示的页面文件(我通常也将其设置为详细信息视图)

4)使属性窗口可见(Menu..View..Properties Window)

5)选择一个用于更新属性窗口的资源文件

应该是这样,但是由于VS布局可以自定义,因此很难给出更精确的说明。



Alan。
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).


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.


这篇关于使用(.NFO)文本文件作为.exe中的资源,如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆