简体   繁体   中英

Import app.config from class library into project when references

I have a class library that has settings within an app.config.

I know that any project that uses this library needs to have settings in it own app.config.

Is there any way to bring the settings from the library as a template/default configuration settings?

I want to give the project a starting point, but also allow for them to override for a specific configuration for their needs.

I would appreciate the help!

I have also needed to do this in the past; what I recall from the research I did at the time it is not possible to mark the file as "required" and copy along with the reference. The advice from other forums and MSDN was to manually merge the App.config files in the final location.

The solution I ended up using as a post-build script on the start-up/default project that XCOPY the App.Config file into the correct target directory. This worked well for my situation as I already had a post-build script in place to do other tasks.

If there is indeed a better/cleaner solution I would be interested to hear it.

Edit: Below is a cut-down version of my post-build script. Hope it helps.

@ECHO OFF
SETLOCAL

:: VISUAL STUDIO USAGE
:: $(ProjectDir)..\PostBuild.bat $(TargetName) $(TargetDir) $(ProjectDir) $(ConfigurationName)

SET TargetName=%1
SET TargetDir=%2
SET ProjectDir=%3
SET Configuration=%4

:: XCOPY Usage
:: /I If destination does not exist and copying more than one file, assumes that destination must be a directory.
:: /F Displays full source and destination file names while copying.
:: /Y Suppresses prompting to confirm you want to overwrite an existing destination file.
:: /D Copy only those files whose source time is newer than the destination time.
:: /R Overwrites read-only files.
:: /S Copies directories and subdirectories except empty ones.
:: /E Include empty directories

:: MOVE Usage
:: /Y Suppresses prompting to confirm you want to overwrite an existing destination file.

:: Copy the *.config to output
ECHO F | XCOPY /F /Y /R PathToReferenceLibary\App.config %TargetDir%ReferenceLibary.dll.config

:CLEAN
ECHO =============================================================
ECHO == Clean Unwanted Files
ECHO =============================================================
:: Delete unnecessary default App.config
DEL /Q %TargetDir%App.config

:: Delete license files as they should *never* be shipped!
DEL /Q %TargetDir%*.lic

:EXIT
EXIT ERRORLEVEL

Can't you just make a default app.config and then the user can change it ? This will make it easier for the user to know what can be changed and what the format of those changes need to look like.

A solution I used in the past was to create a sealed class in my library called something like Configuration which also had a method IsValid . My ClassThatNeedsConfiguration class that was needed by the programmer had a constructor that required the Configuration class passed in. Thus the programmer would create a Configuration class (with some defaults), change it as needed (from app.config, database, or whatever, I didn't really care where the configuration came from) and pass that into the constructor (well it was actually a factory method) to get a valid ClassThatneedsConfiguration class.

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