I'm looking at tidying up my project layout in Visual Studio and I'm wondering if there is any hack, plugin or trick to associate an .xml file with a .cs file of the same name so they appear grouped in my solution navigator/explorer.
Similar to the way the code-behind file is associated with its aspx.
Any suggestions welcome. Thanks
In your project file :
<Compile Include="FileA.cs"/>
<Compile Include="FileA.xml">
<DependentUpon>FileA.cs</DependentUpon>
</Compile>
Or you could use Group Items command of VSCommands 2010 extension.
Edit: Just in case your file is in a folder, don't include the folder name in DependentUpon tag. For example if your file is in Helpers folder:
<Compile Include="Helpers\FileA.cs"/>
<Compile Include="Helpers\FileA.xml">
<DependentUpon>FileA.cs</DependentUpon>
</Compile>
If you do not want to slow down you IDE with heavy and proprietary VSCommands extension you can use small extension NestIn instead. It can nothing but group/ungroup files
In .NET (Core+)
<ItemGroup>
<Compile Update="FileA.*.cs">
<DependentUpon>FileA.cs</DependentUpon>
</Compile>
</ItemGroup>
Note that Update is used instead of Include where files are already (implicitly) included in the project and use of Include causes compile-time "Error NETSDKxxxx Duplicate 'Compile' items were included... The duplicate items were: 'FileA.xxxx.cs' C:\Program Files\dotnet\sdk\x.x.x.x\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.DefaultItems.Shared.targets"
See Is there a DependentUpon option in a .net core app?
For the simple case where the file is a "top level" file, Julien's description works perfectly. However, in the case where the DependentUpon file is in a Folder under the project, this looks different. I personally don't like it because it seems like it could lead to ambiguity, but that's an opinion.
<Compile Include="DataStructs\CKDTree.cs" />
<Compile Include="DataStructs\CClosestObjects.cs" >
<DependentUpon>CKDTree.cs</DependentUpon>
</Compile>
Notice that the dependent item does NOT include the Folder of the parent. This is true in VS2013... probably true in earlier versions but I have not verified it.
File Nesting extension for visual studio is a good one. It has about 500K downloads at the time of writing this answer. I added it personally to my VS 2015 and it was working fine (haven't tried it with VS 2017 yet).
Not sure if people are aware, but nesting files like this seemingly breaks VS's ability to rename the root file, at least when your new nested file is also a partial class. For instance here's the tree we created...
MainWindow.xaml
MainWindow.xaml.cs
MainWindow.Commands.cs
MainWindow.Commands.cs is just another partial class of MainWindow, same as MainWindow.xaml.cs. However, if you then try and rename MainWindow.xaml, instead of automatically renaming the dependent files, it throws an exception.
For completeness, I also tried naming the file MainWindow.xaml.Commands.cs but that didn't work either.
Without the extra 'commands' file, rename works fine, of course.
MainWindow.xaml
MainWindow.xaml.cs
Anyway, this was reason enough for us to abandon nesting files like this. Without the ability to rename, it's just not worth it.
Related
I added a chm help file as link to the application's project but it's file name is not good for releasing to the public ("Compiled help.chm"). Unfortunately it's maintained in a different git submodule by other people and it's name is an automated output from their help builder.
After adding file as link there is no option to change the file name. Is there a csproj xml feature allowing user to rename a file link, possibly without breaking WiX installers depending on it and other undesired consequences?
I was a bit curious about this one, so I gave it a try myself, and I think I was able to get something that will work for you:
If you have the file already in your project as a link, skip to 2; o/w, drag the file over your project in Visual Studio and - while holding down both Ctrl and Shift - drop the file on your project, creating a link.
Close the solution and project
Using notepad or some other text editor, edit the .csproj file, then locate your logical link by looking for the filename you just added.
Edit the link node as follows:
<ItemGroup>
<None Include="....\OtherTeamOutputFolder\Compiled help.chm">
<Link>Super Cool Production Product Name.chm</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
Build your project; witness the glory.
If you control WiX project, you can specify file name in the file element:
<Component…>
<File Id=”FILE_MyProgramDir_SomeAssemblyDLL”
Name=”SomeNewNameForAssembly.dll”
Source=”SomeAssembly.dll”
KeyPath=”yes” />
</Component>
However, you should consider the new name while accessing to renamed resources both from your executable/class libraries and from other stuff.
I have a linked file in my csproj template:
<Compile Include="..\$saferootprojectname$\Shared Files\Mvx$saferootprojectname$.cs">
However, when the template is run, instead of leaving the relative path alone, Visual Studio substitutes the path of the temporary folder that is unzipped (and subsequently deleted!!!). The result is
> Error 1 Source file 'C:\Users\tim\AppData\Local\Temp\mi0qfyf2.5lc\Okra\Shared
> Files\MvxOkra.cs' could not be found \\vmware-host\Shared
> Folders\Documents\Projects\Temp\Plugins\Okra\Okra.Mac\CSC Okra.Mac
since the resulting csproj has
<Compile Include="C:\Users\tim\AppData\Local\Temp\k4psfgjk.22c\Crab\Shared Files\MvxCrab.cs">
<Link>MvxCrab.cs</Link>
</Compile>
instead of the relative path. Is there any way to solve this without resorting to a Wizard?
Update. This can be resolved by adding <CreateInPlace>true</CreateInPlace> into the vstemplate. Then the relative path ends up right. However, I do not want this solution. I would like the solution that allows CreateInPlace to be false (it seems to be default now in VS2012), because my solution is on a network share and it triggers a "do you want to open" prompt for every project in the template (there are 8). (Even when I say don't Ask)
Maybe an alternative solution would be how I can make a network share trusted so this prompt doesn't happen.
I'm working with a php website in Visual Studio 2010. Everything was going well until I tried to publish. None of the php files are included in the published version of the website. How do I fix this?
I read in this thread (Visual Studio 2010 Web Publish missing a file) that you can change the BuildAction property on a per-file basis. Great, but how do I do this automagically for eleventy-two and some-odd files spread throughout a massive folder hierarchy? Could I change the default BuildAction for, say, every file that ends in *.php? I'm even willing to do this on the command-line, because, at least then, I can iterate through a list of the files and change it for each of them.
Edit: I realised something: The BuildAction Property is stored in the <Project_Name>.vbproj file, in this form:
<ItemGroup>
<Content Include="aboutus.php" />
<None Include="login.php" />
This is what it looks like just after I changed the BuildAction of aboutus.php (via VS), but not of login.php. Judicious use of Find-and-Replace would work, but would be tedious. That's my temporary solution.
One way you can change the build action of multiple files through the VS IDE is by selecting all the files you want, then properties, then setting the build action. I know that it definitely works for all files that end in the same extension.
The other thing I would think about is a Visual Studio macro to get the desired result, take a look here
on the MSDN site
I didn't really find a good solution, but I did find a dirty fix. I realised that the .vbproj file--which contains the BuildAction setting, is in XML format. So, I replaced every occurrence of <None ... with <Content ..., and then each </None> with </Content>. This wasn't an ideal solution, because it also affected many files that shouldn't have been included in the published version of the website, but it is what it is.
I did learn, however, from KevDog,s link, that it is possible to write custom file templates for Visual Studio that affect the BuildAction attribute of any new files created with that template. This wasn't appropriate in my case, because it affects only newly generated files, but it might help someone else who sees this problem coming from the beginning of a project--like me, on my next php project.
In Visual Studio, we can "Add as link" to add a link to a file in another project in the solution.
Is there any way to do this for entire folders, so that an entire folder in project A will be visible in project B, without the need to manually link to new items in that folder?
As this blogpost stated, it is possible.
<ItemGroup>
<Compile Include="any_abs_or_rel_path\**\*.*">
<Link>%(RecursiveDir)%(FileName)%(Extension)</Link>
</Compile>
</ItemGroup>
But be aware, the files will not be copied.
In VS2012 and later, you can drag a folder to another project with alt key pressed. It's just the same as adding each file as link manually but faster.
upd:
Consider using Shared Projects if you are using VS2013 update 2 (with Shared Project Reference Manager) or VS2015.
One addition to the answer from mo. and the comment from Marcus, if you are linking content items you will need to include the file extension:
<ItemGroup>
<Compile Include="any_abs_or_rel_path\**\*.*">
<Link>%(RecursiveDir)%(FileName)%(Extension)</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Compile>
</ItemGroup>
Regarding the part of the original query to have a linked folder appear in the IDE, it is kind of possible to achieve this so there is a folder in the solution explorer with all linked files inside, instead of all the files appearing in the root of the solution. To achieve this, include the addition:
<ItemGroup>
<Compile Include="..\anypath\**\*.*">
<Link>MyData\A\%(RecursiveDir)%(FileName)%(Extension)</Link>
</Compile>
</ItemGroup>
This will include all files from the linked directory in a new folder in the solution explorer called MyData. The 'A' in the code above can be called anything but must be there in order for the folder to appear.
If you want to add a folder as a reference and you don't want to compile it, use:
<Content Include="any_path\**\*.*">
<Link>folder_in_B_project\%(RecursiveDir)%(FileName)%(Extension)</Link>
</Content>
Even when there are so many solutions it took me a while to understand it. Here I will try to explain it a little bit more.
I needed link to the whole folder so my final result is:
<ItemGroup>
<Content Include="..\Gym.Management.Api\TestFolder\**\*.*">
<Link>TestFolder\%(RecursiveDir)%(FileName)%(Extension)</Link>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
where:
..\Gym.Management.Api\TestFolder\ represents path to the other project containing the folder I want to link
TestFolder\ in <link> tag is the final(destination) folder in my current project where I want to link it
TIP:
When you are not sure how to get the proper Include path then in your current project right click on project->click Add->Existing item->navigate to one of those files from folder you want to link-> instead of Add, press the dropdown arrow next to it->click Add as link.
This link is inserted in your .csproj file and from there you can extract the Include path.
If what you are looking for is to add another folder to your current workspace for easy access & development experience.
you can do this by just clicking file -> Add Folder to Workspace option in vscode.
Bust out the shell and add a symbolic link.
runas Administrator then
mklink /d LinkToDirectory DirectoryThatIsLinkedTo
BAM symbolic link!
/d specifies directory link.
Works in Vista on up out of the box. Can be backported to XP.
Documentation here: http://technet.microsoft.com/en-us/library/cc753194%28WS.10%29.aspx
For those not familiar with symbolic links, it's essentially a pointer to another file or directory. It's transparent to applications. One copy on disk, several ways to address it. You can also make a "hard link" which is not a pointer to another address, but an actual file ID entry in NTFS for the same file.
NOTE: as stated in the comments, this would only work on the computer where you created the symlink and wouldn't work in a Version Control System like git.
This is driving me nuts. I think it is because of some connection string mismatching across the app (using MVC) but Visual Studio constantly takes my .designer.cs file and recreates it adding a '1' at the end (or 2 if it already did this before argh!). My question is two-fold: Is there a way to stop VS from doing this? But more importantly: How can I manually set it back to the original .designer.cs or is that not possible? I try to delete the new one it created (because I keep getting duplicate definition build errors) and it won't automatically associate back with the original one in the DBML file structure in solution explorer.
Does anyone have a solution for this madness?
Not an ideal solution, but a workaround would be the following. In Visual Studio, go into the properties of the .dbml file and clear out the Custom Tool property. This will prevent it from running the auto-generator against the DBML and creating the designer files. Of course further changes to the DBML won't be reflected in the designer file.
As for why it's generating multiple designer files, that's an oddity. To manually associate a file under another, you need to modify the project file. Look for a line like
<Compile Include="file.designer.cs" />
and change it to
<Compile Include="file.designer.cs">
<DependentUpon>file.dbml</DependentUpon>
</Compile>