简体   繁体   中英

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.

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

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.

  1. Select the text file in the Solution Explorer.
  2. In the Properties panel, set the Build Action property to Embedded Resource

The "Copy to Output Directory setting" just ensures that the file is copied into the Build\\Debug or Build\\Release folder.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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