How do I add files to VS Solution Explorer with a wildcard in the project file? - visual-studio

In my project file I include all of the .cpp and .h files in a directory by using a wildcard as shown in the code snippet below. Everything compiles just fine.
The annoying issue I run into is I have to add files to the Solution Explorer manually via the Add Existing Item dialog. If I don't use a wildcard and spell out every file, I don't have the same problem. Not something I want to do with a boatload of source files though.
Is there a way to get these wildcard includes into the Solution Explorer?
<ItemGroup>
<ClCompile Include="$(SrcDir)\*.cpp" >
<Link>%(FileName)</Link>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="$(SrcDir)\*.h">
<Link>%(RecursiveDir)\%(FileName)</Link>
</ClInclude>
</ItemGroup>

Related

Visual studio 2017 Excludefoldersfromdeployment but include some subfolder

I was wondering if it's possible to include some sub-folder on publication, of an excluded folder.
Let's assume i have this structure:
/Folder1/
/Folder1/FileX
/Folder1/FileY
/Folder1/SubFolder1/
/Folder1/SubFolder2/
What i want is to exclude the whole Folder1 content, but include only a specific set of sub-folder (in my example SubFolder1).
The Folder1 folder is excluded with the ExcludeFoldersFromDeployment in the .pubxml:
<ExcludeFoldersFromDeployment>Folder1</ExcludeFoldersFromDeployment>
You haven't told us what this .pubxml file is, so there is a limit to what we can help you with.
But in general:
The construct that deals with files and folders in MSBuild is Items.
You want an Item here, not an MSBuild property.
So you could easily use an item to point to a specific sub folder in your build environment like this:
<ItemGroup>
<!-- This will grab all files in SubFolder1 but not recursively -->
<DeployThese Include="/Folder1/SubFolder1/*.*" />
</ItemGroup>
You can then do anything you want with that Item. You could copy the files in it somewhere else, or anything else with them.
The files are accessed later by using #(DeployThese)
I was wondering if it's possible to include some sub-folder on
publication, of an excluded folder.
Yes, it’s possible.
Note: One point we should know, as you’ve used “ExcludeFoldersFromDeployment” element in .pubxml to exclude the entire Folder1 directory. Some deploy actions in .xxproj file may be overwritten or affected by it.
So, to achieve your goal we have to follow two steps:
1. Copy the SubFolder to a new folder (A new folder in $(ProjectDir) can be better)
2. Add the content of SubFolder to publish, and choose the structure you want
Here’s a workaround:
1: Add a PreBuildEvent prorerty in .csproj file.
<PropertyGroup>
<PreBuildEvent>xcopy "$(ProjectDir)/Folder1/SubFolder1" "$(ProjectDir)/NewFolder" /E /Y /I</PreBuildEvent>
</PropertyGroup>
2: Add following markup to .csproj file, it helps publish extra files to publish folder in Asp.net:
<PropertyGroup>
<PipelineCollectFilesPhaseDependsOn>
CustomCollectFiles;
$(PipelineCollectFilesPhaseDependsOn);
</PipelineCollectFilesPhaseDependsOn>
</PropertyGroup>
<Target Name="CustomCollectFiles">
<ItemGroup>
<_CustomFiles Include="NewFolder\**\*" />
<FilesForPackagingFromProject Include="%(_CustomFiles.Identity)">
<DestinationRelativePath>MyStructureUnderPublishFolder\%(RecursiveDir)%(Filename)%(Extension)</DestinationRelativePath>
</FilesForPackagingFromProject>
</ItemGroup>
</Target>
For this markup, we only need to change two paths to meet our needs.
First:The CustomFiles refers to the folder whose content will be published.
Second:The MyStructureUnderPublishFolder refers to the structure you want under publish folder. If you want a Folder1 which only has a SubFolder in it after deployment, change it to Folder1/SubFolder1, or change it to SubFolder1 if you want a simple Subfolder1 under Publish folder.
More information about adding extra files to publish see here.

Is there a way to add source files to visual studio project from command-line?

I want to use sublime to edit a visual studio project.
I have a custom build:
{
"cmd": ["c:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\MSBuild.exe"],
"working_dir": "${project_path:${folder:${file_path}}}/../Project"
}
But if I add new files I also need to include them in the project.
Is there a way to do this from the command line, maybe at compile-time?
I am working with opengl using c++;
I basically set up a project using one of the examples provided on the opengl website.
Then I opened the project folder in sublime text and successfully compiled it using the custom build system.
However, when I add NEW source files to the project (*.h and *.cpp) I get a linking error.
I get the same error when I build in visual studio.
The error disappeared after I had included the files by manually browsing and adding them to the project.
What I wanted was a way to automatically add all the source files in a folder to the project(via command line, or wildcard or smth else).
This way I can easily work on a vs2010 project in sublime, add new source files and build the project.
Or maybe there already is a better workflow for this?
You could try to modify your .vcxproj file to include any .h and .cpp file in your project folder or folders below.
In case of a c++ VS project you can try to alter your .vcxproj file like this:
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- rest of project file untouched -->
<!-- start of modified part -->
<ItemGroup>
<ClInclude Include="**\*.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="**\*.cpp" />
</ItemGroup>
<!-- end of modified part -->
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
Be aware that adding files to your project from inside VS at later point will replace the modification described above!
As an alternative you could also create an external project file holding the same <ItemGroup /> elements described above and include this project file into your .vcxproj.
I'll add an example of this alternative if you're interested.

Copy entire directory to output folder maintaining the folder structure?

I want a specific directory to be copied to output folder ("bin") on every build. I think it can be handled via post build scripts. But I'm not sure how to copy a directory itself. I know how to handle specific files.
For eg, this works for a file:
In
Project > Properties > Build Events> Post Build
COPY "$(SolutionDir)Resources\Release Notes.pdf" "$(TargetDir)"
But suppose I have a directory Template, now I need everything under Template to come to bin folder upon successful build maintaining the folder structure.
I tried this:
COPY "$(SolutionDir)Resources\Template\" "$(TargetDir)"
Only the files in Template directory gets copied this way and not the sub directories and the files inside Template folder. I want the folder Template itself to come inside my output bin folder. In other words, bin should look like:
bin > Template > abc.xxx
xxx.yyy
Subdirectory1 > asd.qwe
zxc.qwe
Subdirectory2 > ...
This could be a duplicate, but I couldn't find a relevant thread. Thanks.
I just added this to my *.csproj file (right click Edit Project File)
<ItemGroup>
<Content Include="MYCUSTOMFOLDER\**">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
I think for this the directory needs to be on same hierarchy level as *.csproj file or bellow that.
This worked for me. /S is the key which copies everything recursively.
XCOPY "$(SolutionDir)Resources\Template" "$(TargetDir)\Template\" /S
Since I wanted files to be overwritten every time without a prompt, I added a /Y switch as well.
XCOPY "$(SolutionDir)Resources\Template" "$(TargetDir)\Template\" /S /Y
Try XCOPY instead of COPY; e.g.
XCOPY "$(SolutionDir)Resources\Template\" "$(TargetDir)\Template" /s /i /y
More info on XCOPY here...
http://www.computerhope.com/xcopyhlp.htm
Here's an additional solution working on Visual Studio 2019 as of the date of this post. This will copy the folder structure recursively and all files within. Tested on a C++ .vcxproj in a multi-project solution.
First, start by editing your [ .proj / .vcxproj / .csproj ] file. Once open, find your project scoped tag. If you already have ItemGroups within, then paste the code below directly after the existing ones. Otherwise, add it in before the PropertyGroup tags. Then modify the Include & Link parameters for the folder structure you wish to copy to the output path.
<ItemGroup>
<Content Include="..\Assets\**\*.*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<DeploymentContent>true</DeploymentContent>
<Link>Assets\%(RecursiveDir)\%(Filename)%(Extension)</Link>
</Content>
</ItemGroup>
Note: If you have multiple top level folders, like JS, IMG, BIN, etc., then create a new entry for each one.
The solution by CodingYourLife almost worked for me, but I found out that PreserveNewest was not being respected. I found a solution on the Visual Studio forums that works correctly. My .CSPROJ now looks like this:
<Content Include="assets\**">
<Link>assets\%(RecursiveDir)\%(Filename)%(Extension)</Link>
<TargetPath>assets\%(RecursiveDir)\%(Filename)%(Extension)</TargetPath>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
Note: This solution requires Visual Studio 16.10 or newer.
I have a working solution of this question:
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<ItemGroup>
<CommonFont Include="..\common\src\Web\wwwroot\css\fonts\*.*" />
</ItemGroup>
<Copy SourceFiles="#(CommonFont)" DestinationFolder="wwwroot\css\fonts" SkipUnchangedFiles="true" />
</Target>
This is the only solution that worked for me (VS2022, .Net Framework):
<ItemGroup>
<ContentWithTargetPath Include="..\..\..\Libraries\Core\Business\Vodovoz.Reports\Reports\**">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<TargetPath>Reports\%(RecursiveDir)\%(Filename)%(Extension)</TargetPath>
</ContentWithTargetPath>
</ItemGroup>

Visual Studio, csproj: How can I specify a linked file in DependentUpon?

Suppose I have a default.aspx.cs in my project, but I want to make it dependent upon the default.aspx file that's linked. E.g.:
<Content Include="..\somedir\default.aspx">
<Link>Default.aspx</Link>
</Content>
This doesn't work:
<Compile Include="default.aspx.cs">
<DependentUpon>Default.aspx</DependentUpon>
<SubType>ASPXCodeBehind</SubType>
</Compile>
This also doesn't work:
<Compile Include="default.aspx.cs">
<DependentUpon>..\somedir\default.aspx</DependentUpon>
<SubType>ASPXCodeBehind</SubType>
</Compile>
For both, I get this error:
The parent file, 'Default.aspx', for file 'default.aspx.cs' cannot be found in the project file.
Is it possible to have a file be dependent upon a linked file?
I tried the same thing and it seems is not supported. Check this: https://bitbucket.org/jfromaniello/nestin/issue/4/error-when-nesting-linked-files "DependentUpon does not work with linked files."

How to set Visual Studio to Publish pdf files automatically

Is there a way to set visual studio to publish all pdf files?
I know that you can set each individual pdf file in a project with the Build Action
"Content" property.
But that means doing the same thing 100's of times for my current project, is there a way to change a global setting to do the same thing?
there is an easier way, you have to make sure your file is included in the project first, then right-click on the file go to properties, there will be an option "copy to output directory", choose "copy always"
Good luck
Just right click on the file you want to include, choose properties, in the properties window change build action to content. This will include the file during publish.
Add a post build event with the following command:
xcopy "$(ProjectDir)myPdfs\*.pdf" "$(TargetDir)myPdfs\" /S /Y
Note in the above command myPdfs is just a subfolder of your project directory that contains all the PDF files. If you have more than one of these subfolders you need to run the command for each.
Hope this works!!
Suppose you had the PDFs you wish to deploy outside the project in c:\PDFs, modify the .csproj
<ItemGroup>
<Content Include="c:\PDFs\**\*.pdf" />
</ItemGroup>
If they're in a folder "MyPdfs" relative to the root of the project
<ItemGroup>
<Content Include="MyPdfs\**\*.pdf" />
</ItemGroup>
Some further details about this can be found on: https://stackoverflow.com/a/12202917/37055
Open the csproj file and change :
<None Include="my.pdf">
to:
<Content Include="my.pdf">
You could edit your project file directly to add the required <CopyToOutputDirectory>Always</CopyToOutputDirectory> elements to the PDF files. (If your project isn't under source control, test on a copy first and keep backups in case it all goes wrong)
CopyToOutputDirectory will copy the files to the bin folder when you publish. Setting "Build Action" to "Content" will copy the files without the need of CopyToOutputDirectory setting. But this is still needs to be done on each file. You could make a regex replace in project file from <None Include="XXX.pdf" /> to <Content Include="XXX.pdf" />.

Resources