Im working on this word game in C#, but now I want to get every word that I will use from text file, just not sure how to do it. My problem is about if I send my program to my friend, I still want program to read words from text file, line by line. I tried to include text into resource files but im not sure if this is the right way, Im lost...
Asked
Active
Viewed 881 times
0
-
Will sending the text file to your friend along with your program solve your problem? – Andrew Savinykh Jul 04 '17 at 23:26
2 Answers
1
You can add it to the resources if it's something that is never going to change (on your friend's end), or you can set the file to "copy always" in your project so that it copies the file into the bin
. This should then get packaged along with your executable when you publish it.
-
What is the way to access it then if it is in the bin? can you give me short example of file path? – renekton Jul 04 '17 at 23:31
-
Relative path should work; otherwise, get the current working directory with `System.IO.Directory.GetCurrentDirectory()`, and then construct the path to the file based on that and the relative location. – Jul 04 '17 at 23:50
-
Just one more question, does file has to be exactly in bin folder or in debug folder? – renekton Jul 04 '17 at 23:53
-
When you set the file to copy always it'll put it in the bin under each configuration you've created, so it'll end up copied to Debug, Release, etc. When you publish, the output of the configuration you select will be published to the root of the directory that is created. So if you are running in Debug, the file should be copied to bin/Debug. If the file is in a folder structure inside your solution, the whole structure is copied. – Jul 05 '17 at 00:04
-
so I've set my text file for copy always. but I still can't get to run it. But It works when I manually put it into debug folder. Now Im not sure if this is good practice or not. When I create text file it's automaticly in root folder. Though Im not sure what will happen if I want to make release of my app, though not even sure how that works, Im completly new to c# and visual studio find it quite confusing. I didn't mave much problem with coding now just busting my head with how to read from file. http://prntscr.com/fs1uek this is the file im trying to get – renekton Jul 05 '17 at 18:45
-
So are you're saying that when you set the file to "copy always" in the properties, it's not actually copying it to the output directory? – Jul 05 '17 at 18:48
-
What version of .NET are you targeting? Can you post your csproj file to GitHub's gist or something? – Jul 05 '17 at 18:57
-
https://www.dropbox.com/s/z1dn35clf3exblx/poizkus.zip?dl=0 , I've put it on dropbox hope it works, It's community 2017 – renekton Jul 05 '17 at 19:07
-
When I open your project, the beseda.txt file in in your `bin\Debug` folder, so the copy was successful. This works for when you decide to distribute the application through a publish. For accessing the file, use a relative path or the `System.IO.Directory.GetCurrentDirectory()` mentioned previously. Output to your debug console to make sure your pathing is correct. Then you should be fine. – Jul 05 '17 at 19:20
-
0
You can pass the path to your file using the command line:
e.g. application.exe ##path##
https://stackoverflow.com/a/12998434/1384539
then
class Program
{
static void Main(string[] args)
{
using (StreamReader reader = new StreamReader(args[0]))
{
while (true)
{
string line = reader.ReadLine();
if (line == null)
{
break;
}
Console.WriteLine(line); // Use line.
}
}
}
}

Thiago Custodio
- 17,332
- 6
- 45
- 90