A simple file moving console application where source path and destination path is hard coded right now. I have to run this application in different environment - testing and production with different source path and destination path. how to set those path for copying or moving files. how to code or call those path from config files in program.cs
Asked
Active
Viewed 865 times
-1
-
Use `App.config` values instead of hard-coding. – David Jan 12 '17 at 19:50
-
Or pass as command line args – TheLethalCoder Jan 12 '17 at 19:51
-
Use configuration files type for this because it will help you when you deploy application to different environments when deploying you can set values based on the Environments using deployment tools easily . – Yashveer Singh Jan 12 '17 at 19:54
1 Answers
0
source path and destination path is hard coded right now
Move them to config values. Making use of App.config
for environment-specific values is exactly what config files are for. Something as simple as:
<appSettings>
<add key="BasePath" value="C:\some\path\" />
</appSettings>
In the code you'd get that value from the System.Configuration
assembly. Something like:
var basePath = ConfigurationManager.AppSettings["BasePath"];
(Include any error checking you require to make sure it's a valid path, etc.)
Then when you need to create the full path, you'd include that value when combining with other known values, such as non-changing parts of the path or file names or whatever information you have. Something like:
var sourcePath = Path.Combine(basePath, "/some/middle/part", fileName);
You can then use File
operations on sourcePath
.
Each environment would have its own App.config
file.

David
- 208,112
- 36
- 198
- 279