Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

file-io

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!

like image 480
NewbornNerd Avatar asked Nov 09 '12 23:11

NewbornNerd


People also ask

How do I add a file to a solution in Visual Studio?

Add files to a solution To add an item to a solution, on the context (right-click) menu of the solution node in Solution Explorer, select Add > New Item, or Add > Existing Item. A solution file is a structure for organizing projects in Visual Studio.

How do I add a text file to Visual Studio project?

In the 'Add New Item' dialog, select "Text File" and give it a name (file. txt). Now, for your new txt file to be included in your compiled output, you need to tell Visual Studio how to handle the file during compile time. To do this, select your text file in the 'Solution Explorer' and look at the properties panel.


1 Answers

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.

like image 118
Frank van Puffelen Avatar answered Oct 13 '22 14:10

Frank van Puffelen