Jump to content
  • Advertisement
Sign in to follow this  
chadsxe

How to add a .txt file as an asset - C#

This topic is 3157 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am doing work with a text file and the only way I can figure out how to work with it is by directly adding it to the debug/release folder. Otherwise I am stuck using the full path name when calling it. Is there anyway to get around this? Regards Chad

Share this post


Link to post
Share on other sites
Advertisement
1. Add the text file to your C# project, either by dragging it in or by right-clicking and selecting "Add...".

2. Right-click on the text file, and then click "Properties". In the property grid, set "Build Action" to "None", and then set "Copy To Output Directory" to "Copy always". Then every time you build the project, the text file will be copied to your output folder.

Be careful though, since the windows current directory can change (for example by using OpenFileDialog) and then a relative path won't work. However you can get a full path to your executable by using Application.ExecutablePath, and then using Path.GetDirectoryName to get the folder in which your executable is located. Then just use Path.Combine to combine the folder with the relative path to your text file.

Share this post


Link to post
Share on other sites
Quote:
Original post by MJP
1. Add the text file to your C# project, either by dragging it in or by right-clicking and selecting "Add...".

2. Right-click on the text file, and then click "Properties". In the property grid, set "Build Action" to "None", and then set "Copy To Output Directory" to "Copy always". Then every time you build the project, the text file will be copied to your output folder.

Be careful though, since the windows current directory can change (for example by using OpenFileDialog) and then a relative path won't work. However you can get a full path to your executable by using Application.ExecutablePath, and then using Path.GetDirectoryName to get the folder in which your executable is located. Then just use Path.Combine to combine the folder with the relative path to your text file.


Makes sense,

Thanks

Chad

Share this post


Link to post
Share on other sites
If you're only ever going to be reading from the text file, you can add it to the project's resources (accessible through the "Resources" tab in "Project Properties"). If the file was called somefile.txt you can now gain access to its contents via the string Properties.Resources.somefile.

The advantage of this method is that the file is compiled into your executable, so there's one less point of failure to worry about.

Share this post


Link to post
Share on other sites
Quote:
Original post by benryves
If you're only ever going to be reading from the text file, you can add it to the project's resources (accessible through the "Resources" tab in "Project Properties"). If the file was called somefile.txt you can now gain access to its contents via the string Properties.Resources.somefile.

The advantage of this method is that the file is compiled into your executable, so there's one less point of failure to worry about.


Thanks for the tip. It will come in use in the future. But for now I am reding and writing.

Regards

Chad

Share this post


Link to post
Share on other sites
Quote:
Original post by chadsxe
Quote:
Original post by benryves
If you're only ever going to be reading from the text file, you can add it to the project's resources (accessible through the "Resources" tab in "Project Properties"). If the file was called somefile.txt you can now gain access to its contents via the string Properties.Resources.somefile.

The advantage of this method is that the file is compiled into your executable, so there's one less point of failure to worry about.


Thanks for the tip. It will come in use in the future. But for now I am reding and writing.

Regards

Chad
In which case don't forget that the application's own directory is not normally writeable. [smile]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!