Click here to Skip to main content
14,920,001 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
In the project I am working on, there are ten projects. Several of these have configuration files that have different versions depending on whether the software needs to run on my dev box or in the client's live environment. Note that this does not necessarily correlate with the Debug and Release builds; for example, sometimes I need to run a Release build locally or a Debug build in the live environment.

For convenience, I store the different versions of these files in what is perhaps an unconventional location (it is in fact a folder up at the Solution level). What I want is an easy way to copy these files down into their respective places in the project.

NOTE (added having received a few replies): Often, I copy these files into place not as part of a build but simply using the IDE, as part of my normal workflow, so my method for solving this problem does not need to be tied to the build process. Indeed, it is often convenient to have this as an independent function that I can trigger from the IDE.

Can anyone suggest an easy way to do this?

What I have tried:

I have done a fair bit of reading and done quick research on a few options.

I considered using a plain batch file that I call from a VS shortcut but I find these a bit tedious to debug and the outdated syntax is a pain.

I considered MSBuild but all the documentation I read focuses on building whole projects so I'm unclear how to implement this with the smallest possible footprint on all the stuff Visual Studio does automatically.

I also looked at Cake but it is complaining about my projects being .NET V4.8 and I'm not sure how to install it 'globally' as a tool on the Windows 10 machine. This looked like a decent solution because there is a CakeRunner VS Extension, which would allow me to trigger individual targets within any cake script I add in my Solution file-tree.
Posted
Updated 11-Feb-21 21:00pm
v2

Try Project Properties and Build Events. You can add simple cmd commands to the pre-Build or Post-Build box. The Macros button on the event dialog provides simple shortcuts to the different input and output locations of the project, or you can choose external locations just by providing the full path. I have not done this with .NET projects, but I have with C++ and it works will in the latter case.

It is simple to test by putting echo in front of all the commands and running the build. The results should show up in one of the output windows of VS.
   
Comments
Maciej Los 11-Feb-21 5:52am
   
5ed!
Patrick Skelton 11-Feb-21 8:47am
   
Thanks for the reply. I have done this before and it works well when the commands are tied to the build configurations. I couldn't think of an easy way to implement these in such a way that I can copy the set of files I want irrespective of whether the build is set to Debug or Release. Since these commands run every build, ideally I'd only like to do the copy when the source changes, though I guess copying a handful of files would be fast enough to live without this last bit.
Richard MacCutchan 11-Feb-21 8:52am
   
There are far fewer options in the .NET settings compared to C++ projects. So I guess you will have to use some ingenuity.
Patrick Skelton 12-Feb-21 3:01am
   
I have added a note to the original question, as, looking at replies, I think I may not have been sufficiently clear that any way to solve this problem does not need to be a part of the build process.
Richard MacCutchan 12-Feb-21 3:17am
   
To run it from Visual Studio you still need some project that it will try to build, even if that build does not include any actual compilation. Take a look at some of the 'utility' project types.
In ASP.NET Core you're able to quickly change between development and production versions. See: Use multiple environments in ASP.NET Core | Microsoft Docs[^]

When you work with WinForms, you're able to achieve something like that with project's variable. I usually declare it in Program class:
C#
public static bool isUnderDevelopment = true;

Next, i use some method to load resources depending on isUnderDevelopment value.

Sadly, but i haven't found better solution ;(

[EDIT]
Quote:
Note that this does not necessarily correlate with the Debug and Release builds; for example, sometimes I need to run a Release build locally or a Debug build in the live environment.


Assuming that you need to run your application (ASP.NET or WPF) with different configuration files...

Well, i'd suggest to create - let say - "Loader" - an application, which will enable you to choose environment (configuration option) you want to run.

Loader should:
- read options from configuration file (for example: Loader.config),
- enable user to select specific option,
- copy specific folder/file into destination folder,
- run your application (start process).

Loader.config may look like:
XML
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <options>
    <option key="1" value="Debug"/>
    <option key="2" value="Release"/>
  </options>
  <environments>
    <environment key="1" value="local"/>
    <environment key="2" value="live"/>
  </environments>
  <values>
    <value key="Debug" value="path_to_bin_debug_folder"/>
    <value key="Release" value="path_to_bin_release_folder"/>
    <value key="local" value="path_to_local_configuration"/>
    <value key="live" value="path_to_live_cofiguration"/>
  </values>
<configuration>


As you see, Loader.config file is an xml file, which contains 3 main nodes: {options, environments, values} with few subnodes. Every subnode is a dictionary entry ;) The last dictionary binds options/environments with their values.


This is how i did that when one of my clients wanted to switch between environments. Note: i addedd extra protection in my application: user can't open application itself, it is required to use Loader ;)
   
v4
Comments
Richard MacCutchan 11-Feb-21 6:23am
   
:thumbsup:
Maciej Los 11-Feb-21 6:27am
   
:)
Patrick Skelton 12-Feb-21 3:02am
   
I have added a note to the original question, as, looking at replies, I think I may not have been sufficiently clear that any way to solve this problem does not need to be a part of the build process.
Maciej Los 12-Feb-21 3:11am
   
Is this ASP.NET project or WinForms?
Patrick Skelton 12-Feb-21 6:31am
   
Ideally, I would be able to use this in both an ASP.NET project and a WPF desktop application. I face the same problem in both types of project.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900