简体   繁体   English

如何从解决方案项中添加的.txt文件中读取

[英]How do I read from a .txt file added in solution items

I've added a dictionary.txt file to my solution items and I want to be reading from that file, not from where it exists on my hard drive, so that if someone else opens my project on their computer they will still be able to read from that file. 我已经将一个dictionary.txt文件添加到我的解决方案项目中,我想要从该文件中读取,而不是从我的硬盘驱动器上存在,所以如果其他人在他们的计算机上打开我的项目,他们仍然可以从该文件中读取。

In another question on there they say to go into properties and change the Copy to Output Directory settings, but I don't see that under my .txt file properties (I'm using VS2010): Read a text file from local folder 在另一个问题,他们说进入属性并更改复制到输出目录设置,但我没有看到我的.txt文件属性(我正在使用VS2010): 从本地文件夹中读取文本文件

Currently my code reads like this: 目前我的代码如下:

string[] dictionary = System.IO.File.ReadAllLines(@"dictionary.txt");

Is there something I need to do or some other way I need to reference the file to make sure it's only referencing the file I've added to the project? 有什么我需要做的或我需要引用文件的其他方式,以确保它只引用我已添加到项目中的文件? Or am I missing something more fundamental? 或者我错过了一些更基本的东西?

Thanks! 谢谢!

I assume you've already added the file to your solution and that it shows up in the Solution Explorer in Visual Studio as an item directly under the project you are compiling. 我假设您已经将该文件添加到您的解决方案中,并且它在Visual Studio中的解决方案资源管理器中显示为您正在编译的项目下的项目。

  1. Select the text file in the Solution Explorer. 在解决方案资源管理器中选择文本文件。
  2. In the Properties panel, set the Build Action property to Embedded Resource 在“属性”面板中,将“ Build Action属性设置为Build Action Embedded Resource

The "Copy to Output Directory setting" just ensures that the file is copied into the Build\\Debug or Build\\Release folder. “复制到输出目录设置”只是确保将文件复制到Build\\DebugBuild\\Release文件夹中。

The Embedded Resources action ensures that the files gets bundled into your .exe or .dll file and this is available to your program to load. 嵌入式资源操作可确保将文件捆绑到.exe或.dll文件中,这可供您的程序加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM