Malcolm O'Hare
Malcolm O'Hare

Reputation: 4999

How do I add a text file to an XNA 4 Content project?

I am trying to add a .txt file to the content project in my XNA game project (XNA 4).

When I do 'Add -> New Item...' on the content project I can only choose from 4 options (Bitmap, XML, Effect and Sprite Font).

If I try and force a txt file through typing 'test.txt' in the Name field, Visual Studio 2010 hard crashes (although the file appears, and it has some bytes of data that looks like it tried to force a bmp into it).

Anyone have any ideas about how to do this? I can see in the Windows Platformer Starter Kit for XNA 3 they have txt files in the Content project. Is this not allowed in XNA 4?

Upvotes: 0

Views: 1945

Answers (2)

akn
akn

Reputation: 3722

Move file to your content root directory. In solution explorer on Content project choose Add existing item and add your file.

Now change properties of your text file and set:

Build action: Content
Copy to output directory: Copy if newer

With this, your app should compile without problems and your text file should be always in application content directory.

You can access your file in code by using:

Content.RootDirectory+"//filename.txt"

as file path.

Upvotes: 0

Vincent Koeman
Vincent Koeman

Reputation: 751

Why would you want to add txt files to your content? The XNA content is only meant for files that need some sort of processing. If you want to use some txt file, you should probably just add it to the folder that your game is running in. Alternativeley, just add it as a sourcecode file, so not to the content but to the actual project iself.

Upvotes: 2

Related Questions