I made a "game" for a college project that reads data from .txt file at startup and draws some models according to the data in that file. This is the code I use

using (StreamReader sr = new StreamReader(@"C:\Users\User\Desktop\Linije.txt"))                
                    String linija;                    
                    while ((linija = sr.ReadLine()) != null)
                        red = linija.Split(',');                          
                        model = red[0];
                        x = red[1];
                        y = red[2];
                        z = red[3];
                        podatci.Add(new Vector3(Convert.ToSingle(x),Convert.ToSingle(y),Convert.ToSingle(z)));

As you see, this code fills some variables that are then used to define the model that will be drawn and the coordinates where it will be drawn. The problem that I'm having is that I don't know how to distribute that file to other computers (obviously on another computer it would have another path)? Do you have some advices on how to do this? P.S I tried to put it in the Content and set the Build Action on None, and I can see the file in the content directory, but when I change it, nothing happens (the models don't change as they should)

  • \$\begingroup\$ stackoverflow.com/questions/52797/… \$\endgroup\$
    – James
    Apr 16 '12 at 8:20
  • 1
    \$\begingroup\$ The above link shows you how to get the path to where the exe that is running is currently located. You can then just put the txt file in the same folder or a sub folder from the EXE.. I did not post this as an answer as it seems odd when its just a link to another answer. (Sorry for the double comments. I hit enter when I did not mean to.) \$\endgroup\$
    – James
    Apr 16 '12 at 8:22

if you just want to put the file in the same directory as the exe, you can simply write:

using (StreamReader sr = new StreamReader("Linije.txt"))  
  • \$\begingroup\$ I tried, the file does not show in the directory, although I placed the "Copy to Output directory" to "Copy always" \$\endgroup\$
    – NDraskovic
    Apr 16 '12 at 8:58
  • \$\begingroup\$ It's probably in the Content folder. Open your project's folder in Windows Explorer (it's on the right-click menu) and navigate into `\bin\x86\Debug`. Your compiled project and all its stuff is inside there (plus some .pdb files which are debug files, and not required for redistributing the game). There might be subfolders like a Content folder which will probably contain your file. \$\endgroup\$ Apr 16 '12 at 9:12
  • \$\begingroup\$ you have to manually copy the file to the same directory as the exe... if you want the solution with copy to OutputDir you can write: StreamReader reader = new StreamReader("Content" + Path.DirectorySeparatorChar + "Linije.txt"); \$\endgroup\$
    – Saikai
    Apr 16 '12 at 9:16
  • \$\begingroup\$ Instead of copying files, either change the working directory of the executable used when running in the debugger, or detect when running in the debug output directory and look up into the project's root directory for content files. Copying files is completely unworkable when you have lots of or large content files, and can cause a few other interesting problems (e.g., if you scan all content files, then left-over files from a previous copy after deleting one of them can be problematic). \$\endgroup\$ Apr 16 '12 at 9:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.