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

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."

Related

Why error "FS0239 an implementation of the file or module has already been given"?

Visual Studio is underlining the F# code in one of my source files with a red squiggly. The error message reads, "FS0239 an implementation of the file or module ________ has already been given."
When I search the Visual Studio Solution Explorer, there is only one file or module with the supposedly duplicate name.
What's going on?
I found that my *.fsproj file had several lines duplicated, like this:
<Compile Include="File1.fs" />
<Compile Include="File2.fs" />
<Compile Include="File3.fs" />
<Compile Include="File1.fs" />
<Compile Include="File2.fs" />
<Compile Include="File3.fs" />
Removing the duplicates fixed the problem.
As a side note, I think the duplication was caused by a Git merge.

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

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>

Why is my cpp file not in the ClCompile index, causing LNK2019 and LNK2001 errors?

I was receiving LNK2019 and LNK2001 errors in VS 2010 but not with g++. I searched around looking for reasons but everything talked about making sure the right headers were included, the functions properly defined, etc.
After going to the project directory and opening the .vcxproj file I saw this list:
<ItemGroup>
<ClCompile Include="main.cpp" />
<ClCompile Include="pilElem3.cpp" />
... many lines ...
<ClCompile Include="utlMatrixFull.cpp" />
<ClCompile Include="utlVector.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="pilElem3.h" />
... many lines ...
<ClInclude Include="utlMatrixFull.h" />
<ClInclude Include="utlVector.h" />
</ItemGroup>
<ItemGroup>
<None Include="pilElement.cpp" />
</ItemGroup>
pilElement is the parent class to pilElem3 and pilElem4 which were having the linker error.
I didn't change any of the filters or alter the project since creating it.
a) Why did VS think this file should be ignored?
b) How could I notice this sooner?
In answer to how I suspect that you may have inadvertently right clicked on the file in the project explorer - all to easy to do in VS.
How to make sure it doesn't happen in the future you have 2 options a) Don't use visual studio or b) Use a revision control system such as Mercurial and include the project file in those you are controlling - and do regular commits - you will be able to revert changes to the project file if needed and you should notice if it is in the changed file list when you are not expecting it to have changed.

Visual Studio Templates and environment variables

I have created a project template for Visual Studio 2010, and I have an environment variable I setted up in every machine I use. The template references some resources which are located in the directory pointed in that environment variable.
A sample reference in the template is:
<ItemGroup>
<Compile Include="$(MyVariable)OneDir\MyFile.txt">
The key point is that when I create a proyect with this template, and in the .csproj I get:
<ItemGroup>
<Compile Include="..\..\..\..\Users\MyUser\AppData\......."/>
In the same .csproj I have replace it in the .csproj file whit:
<ItemGroup>
<Compile Include="$(MyVariable)OneDir\MyFile.txt">
So, is there any way to make the csproj writes "$(MyVariable" in the "Include" parameter?.
I have tried with the scape caracter %24 which correspond to the "$" character like it is saying in msdn [1]: http://msdn.microsoft.com/en-us/library/bb383819.aspx but i get this .csproj:
<ItemGroup>
<Compile Include="%24..\..\..\..\Users\MyUser\AppData\......."/>
Try workaround used in this post. It is about creating property from env variable and using it inside ItemGroup.

msbuild include / copy from subdirectory to root directory - possible?

Can I include a file from a subdirectory, but have it placed in the root directory of the bulid instead of its original sub directory?
Example: We have multiple robots.txt files for different release configurations. The files are in the solution as \IncludeTest\Development\robots.txt and \IncludeTest\Production\robots.txt
I can dynamically grab them appropriately using something like:
<ItemGroup Condition=" '$(Configuration)' == 'Development' ">
<Content Include="IncludeTest\Development\robots.txt">
<SubType>Designer</SubType>
</Content>
</ItemGroup>
but when I do that, I maintain the \IncludeTest\Development\ (or \IncludeTest\Production) directory. Any way to just include it in the root directory, where robots.txt is supposed to be?
The above still did not work for me entirely, but I was able to find a work around based on how you set up the itemgroup:
Including the file in the solution as a link puts it in the root directory. And with your $(Configuration) hint, I was able to do this, and just include it dynamically as a link, rather than copy it over to the root.
<Content Include="..\Robots_Source\$(Configuration)\robots.txt">
<Link>robots.txt</Link>
</Content>
Not sure I got your question right, let me know whether this work as you expected:
<ItemGroup>
<Content Include = "IncludeTest\$(Configuration)\robots.txt">
<SubType>Designer</SubType>
</Content>
</ItemGroup>
Copy to root:
<Copy SourceFiles="#(Content)"
DestinationFiles="#(Content ->'..\%(RecursiveDir)%(Filename)%(Extension)')" />
EDIT: It could be problem when files are in nested directories, so try out:
<Copy SourceFiles="#(Content)"
DestinationFiles="#(Content ->'$(MSBuildProjectDirectory)..\%(RecursiveDir)%(Filename)%(Extension)')" />

Resources