简体   繁体   中英

How to have “optional” files in a Visual Studio solution?

I am trying to add optional per-developer config settings to my solution using NConfig. I'm telling NConfig to use Config\\Custom.config as its override file. However, I want this to be optional . So if the file doesn't exist it will just fall back to using the setting in Web.config .

NConfig handles this OK, but the trouble is that if the file doesn't exist the solution doesn't build. I need to have Visual Studio copy the file to the output directory so NConfig can see it, but if I add the file reference to the solution and set "Build action" to "Copy always" or "Copy if newer", I get an error if the file doesn't exist on the file system.

How can I have an "optional" file in my solution where it may or may not exist, and if it does exist it copies the file to the output directory?

By using the macros for the build events, you can specify the paths of the source and destination according to the current path structure of your solution:

In the post-build event, use the following script:

if exist "$(ProjectDir)"Custom.config xcopy "$(ProjectDir)"Custom.config "$(TargetDir)Config\" /y

Note the quotation marks around the macro markers. If your directory structure contains spaces, the quotation marks will account for them. This script will copy the file to the "Config" subfolder (creating that folder if it doesn't exist) in the current output directory for your solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM