when refering to a library assembly, it is auto copied to the final bin directory. However the *.dll.config is not, is there a way to automatically copy this config file?
There is a better answer in the following post:
Visual Studio/MSBuild copy referenced class library's app.config as *.dll.config to bin folder of current project
// Add this <ItemGroup> at the end of your .csproj file before </Project> and after <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<ItemGroup>
<Content Include="app.config">
<Link>$(TargetName).dll.config</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
Rename the app.config in the dll project to [dllname].dll.config. Change the properties of the same file to "copy always" and rebuild. Works great for me. I also tried adding another setting through the Settings dialog in VS and it updated fine even though it was not app.config anymore. BTW Using VS2013 update 3.
Another way to do a similar thing is to create a hardlink file with the [dllname].dll.config in the project folder and the change the properties on that to "copy always" that way you still have your app.config as normal but if you updated it, it will also update your [dllname].dll.config file. The only real caveat here is that it might not track the hardlink when you check into source control like TFS and you might accidentally overwrite the link. But then again it does honor links when you "add and existing item" as a link so it may or may not work for your situation. You decide.
In Visual Studio, there's a 'properties' section when you have the active file open. For the App.config, it has a property called Copy to Output Directory
, the options are
Do Not Copy
Copy if Newer
Copy Always
You want either the second or the third option.
With the default CLR host, only an EXE can have a .config file. Not DLLs. Copying a .dll.config file ought to be pointless, unless you open and parse the file yourself explicitly. In which case you ought to use a different name to avoid confusion.
I had the same problem. You can have a situation when Properties will not help you to copy app.config where you need it. Such as when you do integration unit testing. Then you can have two choices:
Use post build event
copy /Y “$(ProjectDir)App.config” “....\test\Debug\$(TargetFileName).config”
Manually copy config file sections from libA.dll to libA.Test.dll
I end up using the 2 as it proved to be more robust for me.
Although I am late, but my answer can help others. I used the following command as pre-build event:
copy /Y $(SolutionDir)\[YOUR_LIBRARY_PROJECT]\app.config $(ProjectDir)$(OutputPath)[YOUR_LIBRARY_NAME].dll.config
I tried to be dynamic as much as possible, and the above command worked.
精彩评论