2

I have a program where it requires a text file to save some information, I have functions that currently use the path of the file then do stuff with it. However, I want to build the program into a .exe, but then the file is obviously not going to be in the same place. After research, I found using embedded resources allows me to add the text file to the program, but I can't seem to do anything with it. I have the "highscores.txt" in embedded files, and am currently using this:

string path = Resources.highscores;

However, this basically says that resources doesn't exit and I don't know what the problem is.

  • 1
    Did you add the `highscores.txt` to the Resources file or did you just simply create a text file in the Solution and set it as `Embedded Resource` in its properties? – MindSwipe Dec 11 '18 at 11:23
  • I added an existing file, which was highscores.txt I think – Dom Beasley Dec 11 '18 at 11:23
  • Would be Properties.Resources.highscores. But do consider that there is no point to this, resources are read-only and surely you want to change this file when the user gets a new high score. Store the file in AppData. – Hans Passant Dec 11 '18 at 11:51
  • If I store it in app data, surely the path will change when moved onto another computer and therefore won't work – Dom Beasley Dec 11 '18 at 12:02

1 Answers1

1

You can't change a file at run time if it's marked as an embedded file. Depending on what you are trying to save you could use the Settings file.

RichardMc
  • 844
  • 1
  • 9
  • 33
  • What's being saved is a list of names and scores in this format "name score" – Dom Beasley Dec 11 '18 at 11:31
  • @DomBeasley In that case I'd advise against trying to use the settings file, just allow your application to save to disc, it would be bad practice to attempt something hacky just to keep it to one file. – RichardMc Dec 11 '18 at 11:40
  • Is there a way I can make a new file every time the exe is sent to someone which is then reused everytime that person uses their version of the game – Dom Beasley Dec 11 '18 at 11:45
  • Plenty of ways to do that https://stackoverflow.com/questions/9907682/create-a-txt-file-if-doesnt-exist-and-if-it-does-append-a-new-line – RichardMc Dec 11 '18 at 11:48
  • Sorry if I'm just not reading it right but it seems to still have a set path, surely that path will completely change if I move it to another computer – Dom Beasley Dec 11 '18 at 11:53
  • @DomBeasley Thats no problem, with time you will know how to get to this information easier. https://stackoverflow.com/questions/3259583/how-to-get-files-in-a-relative-path-in-c-sharp use that to get your path and combine it with creating your file. – RichardMc Dec 11 '18 at 12:02
  • Thanks, I'll look into it – Dom Beasley Dec 11 '18 at 12:06