3

Hey all quick question from someone new to C#/programming. I have a console app where one of my string variables in the job is a path to a directory. However, I am always changing it between 2 different paths (1 prod and 1 dev). So I constantly need to recompile/rebuild the solution when I want to change the hard-coded path.

Is there a way to define it somehow in the config and then a variable in the job can point to it somehow? I'd like to just get away from having the path hard-coded in the solution.

Any advice would be appreciated. Thank you!

Christoph Fink
  • 22,727
  • 9
  • 68
  • 113
user3494110
  • 417
  • 2
  • 9
  • 25
  • You are looking for application settings. See: http://msdn.microsoft.com/en-us/library/bb397750(v=vs.110).aspx – Edin May 22 '14 at 14:38

3 Answers3

2

That is what settings are for. Easiest way to add them is via the project properties:

enter image description here
After you added them you will get a grid to enter a default value.
This value can be accessed in code via:

var path = MyApp.Properties.Settings.Default.PATH;

The default value is stored in the app.config file, which is right next to the MyApp.exe as MyApp.exe.config. If you choose the setting to be "user-changeable" a changed value (after calling MyApp.Properties.Settings.Default.Save();) is stored in %AppData%.

See THIS question on how to transform the app.config depending on build-type.

Community
  • 1
  • 1
Christoph Fink
  • 22,727
  • 9
  • 68
  • 113
1
  1. Add an app.config file to your project by right-clicking it in Solution Explorer. app.config is XML file.
  2. In app.config, under section, add your path like this: enter image description here

  3. Access path in code like this:

    string path = ConfigurationManager.AppSettings["path"];

  4. Then just change the value inside app.config when you want to point to a different path, without recompiling your code.

bytefire
  • 4,156
  • 2
  • 27
  • 36
  • This is about the same as what my suggestion does "behind the covers", but is not as easy to use and error-prone (e.g. no compile-time check if the key exists) => NOT the best recommendation for beginners like the OP stated he is IMHO... – Christoph Fink May 22 '14 at 15:20
  • There are pros and cons. This is an alternative to your good solution. This is self-contained and introduces a new C# developer to how the settings actually work :) – bytefire May 22 '14 at 15:22
  • You are right with `introduces a new C# developer to how the settings actually work` and because of this it is also a good answer here, but I see no "pros" in this case, only cons (no compile-time check, hard-coded "key"-string, ...) - what "pro" am I not seeing? – Christoph Fink May 22 '14 at 15:29
0

The alternative to chrfin's answer is to define the variable as so

#if Debug
   string myPath = "something";
#else
   string myPath = "something else";

then when you choose your build type it will change the line of code that is compiled.

Sayse
  • 42,633
  • 14
  • 77
  • 146
  • I agree @chrfin, but it seems strange to include a development variable into the app.config – Sayse May 22 '14 at 14:42
  • This would still need recompiling. And that is explicitly not wanted by the author. – Edin May 22 '14 at 14:43
  • @Edin - The OP would be creating a debug and release version anyway I would believe so it shouldn't need to be constantly changed in code – Sayse May 22 '14 at 14:45
  • Yes, but you are not able to switch the path in the compiled version, be it Debug or else. You need at least two compilations. On the other hand, what if I want to test a Release version with the Dev-Server? I might want to do that, for all kind of reasons, performance tests, behavior tests etc... You are bundling build configuration to the environment which is not a good idea in my opinion. – Edin May 22 '14 at 14:48
  • I don't think any folder paths should ever be hard coded but that isn't the question here. @edin - I explicitly state this is an alternative to chrfin's answer as it is something that the OP may not be aware of – Sayse May 22 '14 at 14:52
  • I see, but offering it as an answer could be a bit misleading :) – Edin May 22 '14 at 14:55