Automatic Build action for json files - c#

I have a C# "Class Library" type project in Visual Studio 2015.
At the moment when i add in the project files like : json files or pictures, Visual Studio sets the build action for these files as "None" . I need them to be set as "Content".
At the moment i am making this seeting manually. However i am adding a large amount of files of that in my project.
Is there a way i can instruct visual studio to set automatically the build action to "Content" when i add them ?
Also i need this option to be valid also on the TFS machine .

From this msdn article you can see that all files which are included in the build process have to be specified in a .<something>proj file, which is nothing more than a simple XML file.
For what you need I would unload the project within Visual Studio and open up the project file for edit. Find <ItemGroup> in that XML file and add <Content Include="Path-to-your-JSON-files\*.json" />.
<ItemGroup>
<CSFile Include="main.cs" />
<!-- assume you have a bunch of these -->
<Content Include="Path-to-your-JSON-files\*.json" />
</ItemGroup>

Related

How do I include .deps and .runtimeconfig files as project dependencies?

Visual Studio creates two files along with the .exe for my project that are required to run the exe: a deps.json and a runtimeconfig.json. A second project in my solution has the first project as a project reference, but those two files aren't being copied to my second project's output directory. How can I tell Visual Studio that it should copy these files into the output directory of the second project, because the referenced project depends on them?
Output directory of my first project:
Foo.exe
Foo.deps.json
Foo.runtimeconfig.json
Output directory of my second project:
Bar.exe
Foo.exe
Should contain deps and runtimeconfig files, but does not
The solution I found is to manually edit my .csproj file to add the following target:
<Target Name="AddRuntimeDependenciesToContent"
Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'"
BeforeTargets="GetCopyToOutputDirectoryItems"
DependsOnTargets="GenerateBuildDependencyFile;
GenerateBuildRuntimeConfigurationFiles">
<ItemGroup>
<ContentWithTargetPath Include="$(ProjectDepsFilePath)"
Condition="'$(GenerateDependencyFile)' == 'true'"
CopyToOutputDirectory="PreserveNewest"
TargetPath="$(ProjectDepsFileName)" />
<ContentWithTargetPath Include="$(ProjectRuntimeConfigFilePath)"
Condition="'$(GenerateRuntimeConfigurationFiles)' == 'true'"
CopyToOutputDirectory="PreserveNewest"
TargetPath="$(ProjectRuntimeConfigFileName)" />
</ItemGroup>
</Target>
This solution came from https://github.com/dotnet/sdk/issues/1675#issuecomment-658779827.
There were other somewhat similar solutions posted in that thread, but this is the only one that worked for me. The others would either not consistently copy the files to my second project, or cause the first project to fail to build due to attempting to access a file that didn't yet exist. The key difference with this one is the inclusion of the correct "BeforeTargets" property (and possibly also "DependsOnTargets"), controlling at which point in the build process the files are included.

How can I exclude a file, added by a referenced Nuget package, from my build output? [duplicate]

I have a Visual Studio 2008 C#/.NET 3.5 project with a post build task to ZIP the contents. However I'm finding that I'm also getting the referenced assemblies' .pdb (debug) and .xml (documentation) files in my output directory (and ZIP).
For example, if MyProject.csproj references YourAssembly.dll and there are YourAssembly.xml and YourAssembly.pdb files in the same directory as the DLL they will show up in my output directory (and ZIP).
I can exclude *.pdb when ZIP'ing but I cannot blanket exclude the *.xml files as I have deployment files with the same extension.
Is there a way to prevent the project from copying referenced assembly PDB and XML files?
I wanted to be able to add and remove referenced assemblies in my primary application while avoiding the the need to maintain which files I needed to delete or exclude.
I dug through Microsoft.Common.targets looking for something that would work and found the AllowedReferenceRelatedFileExtensions property. It defaults to .pdb; .xml so I explicitly defined it in my project file. The catch is that you need something (whitespace is not sufficient) otherwise it will still use the default.
<Project ...>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
...
<AllowedReferenceRelatedFileExtensions>
<!-- Prevent default XML and PDB files copied to output in RELEASE.
Only *.allowedextension files will be included, which doesn't exist in my case.
-->
.allowedextension
</AllowedReferenceRelatedFileExtensions>
</PropertyGroup>
You can also specify this via the command line:
MsBuild.exe build.file /p:AllowedReferenceRelatedFileExtensions=none
You can add a Post Build event command similar to del "$(TargetDir)YourAssembly*.xml", "$(TargetDir)YourAssembly*.pdb"
top 2 answers didn't work for me. I found a solution in this link which worked for me. http://kitsula.com/Article/How-to-exclude-xml-doc-files-from-msbuild.
Just in case, the above link is not working:
Unload project in Solution Explorer
Right click the project and click 'edit *.csproj'
Add next lines in the PropertyGroup section of each environment.
Reload and rebuild project.
<AllowedReferenceRelatedFileExtensions>
*.pdb;
*.xml
</AllowedReferenceRelatedFileExtensions>
This is a rather old question, but since there is no answer about how to turn off generating PDB and XML files via UI, i figured that it should be here for completeness.
In Visual Studio 2013: in project properties, under compile tab, uncheck "Generate XML documentation file", then click on "Advanced Compile Options" below that and change "Generate debug info" to "None", and that will do the trick.
I didn't have much luck with the other answers, I finally figured out how to do this in my implementation by using the built in "Delete" command, apparently there is a specific way you need to implement wildcards, it's bit nuanced, here's everything you need to be put into your "CSPROJ" (TargetDir is a built in variable, included automatically) under the "Project" tag:
<Target Name="RemoveFilesAfterBuild">
<ItemGroup>
<XMLFilesToDelete Include="$(TargetDir)\*.xml"/>
<PDBFilesToDelete Include="$(TargetDir)\*.pdb"/>
</ItemGroup>
<Delete Files="#(XMLFilesToDelete)" />
<Delete Files="#(PDBFilesToDelete)" />
</Target>
I've also had trouble with various language specific folders being generated, if you have that issue too, you can also remove unused language specific folders too. I've chosen to only trigger this under the build type "Release":
<ItemGroup>
<FluentValidationExcludedCultures Include="be;cs;cs-CZ;da;de;es;fa;fi;fr;ja;it;ko;mk;nl;pl;pt;ru;sv;tr;uk;zh-CN;zh-CHS;zh-CHT">
<InProject>false</InProject>
</FluentValidationExcludedCultures>
</ItemGroup>
<Target Name="RemoveTranslationsAfterBuild" AfterTargets="AfterBuild" Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<RemoveDir Directories="#(FluentValidationExcludedCultures->'$(OutputPath)%(Filename)')" />
<ItemGroup>
<XMLFilesToDelete Include="$(TargetDir)\*.xml"/>
<PDBFilesToDelete Include="$(TargetDir)\*.pdb"/>
</ItemGroup>
<Delete Files="#(XMLFilesToDelete)" />
<Delete Files="#(PDBFilesToDelete)" />
</Target>
My answer might be trivial now but I'd like to share the BAT script I use to delete the xml files if there's a corresponding dll for it. It's useful if you just want to cleanup the output folder and has other xml files that don't want to remove.
SETLOCAL EnableDelayedExpansion
SET targetDir=%1
ECHO Deleting unnecessary XML files for dlls
FOR %%F IN (%targetDir%*.xml) DO (
SET xmlPath=%%~fF
SET dllPath=!xmlPath:.xml=.dll!
IF EXIST "!dllPath!" (
ECHO Deleting "!xmlPath!"
DEL "!xmlPath!"
)
)
Usage:
Cleanup.bat c:\my-output-folder\
It took me an hour to finish this simple work (thanks to the "delayed expansion" stuff) with all type of searching here and there. Hope it helps other BAT newbies like me.
If you only want to exclude the XML files (for say a debug release) you can do something like this:
<AllowedReferenceRelatedFileExtensions>
<!-- Prevent default XML from debug release -->
*.xml
</AllowedReferenceRelatedFileExtensions>
Basically, each extension (delimited by a semi-colon) listed will be excluded.

YAML file doesn't get added to bin\debug on build (.NET Core)

I have a _config.yml YAML file in the root of my project.
I do in Startup.cs:
var builder = new ConfigurationBuilder()
.SetBasePath(AppContext.BaseDirectory)
.AddYamlFile("_config.yml");
Configuration = builder.Build();
When I build and run the project I get the following error:
System.IO.FileNotFoundException: The configuration file '_config.yml' was not found and is not optional. The physical path is 'C:\Users\user\OneDrive\Documenten\Descent.Bot\bin\Debug\n
etcoreapp3.1\_config.yml'.
Shouldn't by building the project the YAML file automatically getting added to bin\Debug? Is there a way to add YAML file in bin\Debug without me manually adding it myself?
You can just right click the the file in Visual studio and click properties.
There you will see option to copy the file into output directory.
Under the hood, Visual studio is creating a None in .csproj to handle the flow
You can read it more here
https://social.technet.microsoft.com/wiki/contents/articles/53248.visual-studio-copying-files-to-debug-or-release-folder.aspx
Right-click on the file in Visual Studio and set the Build Action to Content and the Copy to Output Directory property to Copy if newer.
This will add the following markup to your project (.csproj) file, which will tell the SDK to copy the file to the output folder when you build the application:
<ItemGroup>
<Content Include="_config.yml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
You may of course also add the above to the .csproj file manually by editing it directly (Project->Edit Project File in Visual Studio).

VS 2015 project not rebuilt when I delete a file

I have a config file in my C# project that is excluded from source control (it's local to each developer). However, if the file doesn't exist I want it to be copied from the corresponding ".template" file (which is in source control). To do this, I added an MSBuild item type and target, like this:
<ItemGroup>
<AvailableItemName Include="LocalConfigTemplate" />
</ItemGroup>
<Target Name="CopyFromTemplateIfNeeded" BeforeTargets="BeforeBuild" Inputs="#(LocalConfigTemplate)" Outputs="#(LocalConfigTemplate->'%(FileName)')">
<Copy Condition="!Exists(%(LocalConfigTemplate.Filename))" SourceFiles="#(LocalConfigTemplate)" DestinationFiles="#(LocalConfigTemplate->'%(FileName)')" />
</Target>
(Based on How to hide files generated by custom tool in Visual Studio) This is in a separate file, included from the project file (before Microsoft.CSharp.targets, if that matters).
It works, but when I delete the target file (local.config) and build the project VS thinks it's "up to date" and does not build. How do I get it to detect that the output file is missing and build in that case?
Try doing a Clean before the rebuild, maybe Visual Studio is picking the file up from the bin directory.

ensuring c# project dependencies are copied to output directory in Visual Studio and TFS

I am working on a project which has many dependencies which are developed on a separate team from me. We use TFS 2010. Many of my applications depend on libraries and xml files which are under active development, so I want to keep them up to date. I also don't want to create separate copies of the dll's and xml files for each application/project, but rather source them from their respective locations within the same source control repository. This should be possible using a relative path.
I tried putting the following in my .csproj file
<ItemGroup>
<Dependencies Include="..\..\Driver\Driver.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Dependencies>
</ItemGroup>
this doesn't work, either on my workstation or on the build server, however, the files show up as dependencies in the Solution Explorer, and it allows me to change the copy to output property and shows the full path the to files, which is valid.
Another thing I tried was just running xcopy as a pre-build event, which works on my local machine but does NOT copy the files to the output/TFS drop folder, so it isn't picking it up as a dependency.
Try using the Private Element instead, set to True.
<ItemGroup>
<Dependencies Include="..\..\Driver\Driver.dll">
<Private>True</Private>
</Dependencies>
</ItemGroup>
See http://msdn.microsoft.com/en-us/library/bb629388.aspx

Categories