0

In my c# program I need to read 1 string (the string is a file path). Occasionally I need to write over this file path with a different one. Creating a .txt file as a resource seemed like a good idea but as I found out it is not a good way to write something to. My only other thought is to get a database involved but that seems like overkill for just 1 string.

Is there a better way to do this?

mrsargent
  • 2,267
  • 3
  • 19
  • 36
  • 1
    Why is writing to a text file not a good way? – crthompson Mar 05 '15 at 22:49
  • 2
    Could you please explain in a bit more depth what are you trying to achieve? – Dasanko Mar 05 '15 at 22:50
  • 2
    1) A configuration file. 2) An .INI file. 3) A registry entry. 4) A text file on disk. Search here for `[c#] store config info` - this question has been asked many times before. Here, I [found one for you](http://stackoverflow.com/q/16594730/62576). – Ken White Mar 05 '15 at 22:51
  • 1
    Unless the path is sensitive for some reason, consider using app.config or add a "Settings" file to your project. These are both easy to read in code and are standard ways to have configurable settings for your program – Rufus L Mar 05 '15 at 22:58

2 Answers2

1

Assuming what you're after is a string within the lifetime of the application, using a static string in your program would be the best way to go. You'll also want to do this even with the selections below, as reading from an external data storage for every operation is inefficient, but often you only read when the program starts up and keep the value in memory after that.

If the string needs to exceed the lifetime of your application, which is to say be available and consistent as the program is opened and closed, then you need external data, be it a database, registry entry, or good old config file.

Going the easiest route, a config file should be somewhere your program can always get to it, doesn't move, and most configs lie outside the user's typical areas (don't store it in My Documents). "%APPDATA%\YourProgramName\Config.xml is the most obvious location. See ExpandEnvironmentVariables for turning those variables into usable locations.

David
  • 10,458
  • 1
  • 28
  • 40
  • The first line of your post makes a *big* assumption. – crthompson Mar 05 '15 at 22:58
  • Which is why the second paragraph goes into details about what to do *in the other case* – David Mar 05 '15 at 23:23
  • A settings entry would work much easier than a config file. Clearly however, OP has no interest in following up with a question that can be answered properly. – crthompson Mar 06 '15 at 17:50
  • @papgomez I'm not exactly sure what you mean by "settings entry," so *I* would at least welcome another answer in such a direction. – David Mar 06 '15 at 20:08
  • [Settings](https://msdn.microsoft.com/en-us/library/aa730869(v=vs.80).aspx) and an [answers](http://stackoverflow.com/questions/16881415/c-sharp-settings-default-save-not-saving) [in such a](http://stackoverflow.com/questions/453161/best-practice-to-save-application-settings-in-a-windows-forms-application) [direction](http://stackoverflow.com/questions/5789252/properties-settings-default-save-where-is-that-file) – crthompson Mar 06 '15 at 20:24
0

if you don't want to write a string in a text file, write it in xml format or try using Xmlserializer to serialize your class.

And if only one string is involved then try using static property and access it with static class for global use in your application

David
  • 10,458
  • 1
  • 28
  • 40