Automatically Include generated file in Visual Studio Project - c#

I'm writing an application that will enable the creation of SQL files within visual studio. The user will enter commands via the Package Manager console which will generate sql scrips and deposit them in a specific directory within the Visual Studio Project.
The problem I have is that, when the files are generated, they are present on the file system, but not in Visual Studio. This is expected of course, as I need to then go and actively include the files within Solution explorer, but this isn't what I want. I want the files to "Magically" appear in solution explorer immediately after they're generated.
I've seen various solutions to similar problems mostly featuring amendments to the .csproj file such as this
<Compile Include="Sql\**\*.sql" />
but this isn't what i'm looking for. What i'm after is similar to how, for example, Entity Framework or MvcScaffolding work, where files / folders just magically drop into the project when commands run in PMC. I'm aware this runs off T4 templating, but that seems like too complex a solution for a simple issue like this.
I should qualify that there's no voodoo going on in the creation of the files, just plain old File.Create() stuff.
I'm open to any suggestions.
Thanks.

Check out this answer for a solution that worked for me. I have the same use-case where code outputs flat files and I need to include this output in the project.
At the end of your .csproj file add the following:
<Target Name="BeforeBuild">
<ItemGroup>
<Content Include="Sql\**\*.sql" />
</ItemGroup>
</Target>

IMHO, T4 is the way to go. You don't want to be bothering with older technologies for what you are trying to do.
Having said that, I wonder why is it required for the files to be added to the solution explorer? is it for source control purposes? (usually you don't want to source control auto generated files, you want to source control the original model).
Note that you could always click the 'show all' button and the files will appear in the solution explorer, without actually being a part of the solution.

Related

Include Content in csproj with a wildcard

First the problem I'm trying to solve. I work on a large team and we are constantly having merge conflicts on our web.csproj file. The solution I was trying to implement was including content files using a wild card.
I'm trying to include all files in a directory in my web.csproj file like so.
<Content Include="Areas\Public\Client\**\*.js" />
<Content Include="Areas\Public\Client\**\*.html" />
<Content Include="Areas\Public\Client\**\*css" />
This works great at first glance. I can see all files matching these patterns in visual studio. The problem is, if someone deletes a files from Visual Studio, the IDE then enumerates ALL files in the csproj file and removes my wildcard lines above.
Have any of you solved this problem or have any suggestions?
Thanks!
This is not possible in VS 2015 without loosing the functionality to add/rename/move/etc. items and keep the wildcards.
The handling of globbing in project files is only impelmented by the new CPS-based project system in VS 2017, which is not (yet) used for "classic" .NET / ASP.NET projects.
There are ways to add the Content items during the build using globbing patterns (using custom targets) but these will not show up in the solution explorer (which is okay for auto-generated files)

How to add/update references without Visual Studio using a custom script?

I am trying to build a previously created .NET application (VS2015 Community), but before building I have to solve several issues manually. One of these issues is to add/update reference to System.Web.Mvc.dll which is located in local project folder.
In Visual Studio 2015, this is fairly easy. Just click References and then proceed to Add reference, where you Browse the correct reference. However, I want to perform this step without using Visual Studio with my custom script, which performs this automatically.
Is this even possible to do without Visual Studio? If it is, what other requirements should be taken into account in general?
I am new with Visual Studio programming (mainly programmed with GNU/Linux), so I am not aware of all limitations or possibilities regarding to VS and Microsoft BuildTools.
Thank you.
The references are listed in the project file.
I've never been able to find proper documentation for those (but see here: http://msdn.microsoft.com/en-us/library/5dy88c2e.aspx and here: https://msdn.microsoft.com/en-us/library/dd576348.aspx, but when you inspect it it should become clear. You'll need to add to the ItemGroup which includes the references. It'll look something like this:
<ItemGroup>
<Reference Include="System" />
...
</ItemGroup>
How you want to update the file is up to you, but an XSLT transformation seems a reasonable way, or your script could just do raw xpath stuff

How to automatically delete Test Results

I run tests several times a day in Visual Studio 2012. I recently found that my disk space was very low. I found that the test results folder in my project was using 60 GB. I deleted the files, but I want to keep it from happening. I did a search for how to do this, but all I can find are solutions for 2008 and 2010. They stated I need to make some changes to the test tools in the options. I can't find this inside of my options. How can I keep from these files appearing, or keep them to a minimal?
Mark Seemann suggests extending the Clean target
Add this after the Import element at the end of the project file:
<PropertyGroup>
<TestResultsFolderPath>..\TestResults</TestResultsFolderPath>
</PropertyGroup>
<Target Name="AfterClean">
<RemoveDir Directories="$(TestResultsFolderPath)" Condition="Exists('$(TestResultsFolderPath)')" />
</Target>
Then whenever you want to manually remove the test results, you can just right-click in the Solution explorer and select Clean.
You can also achieve the same from the command line with the following
MSBuild /t:Clean MyProject.csproj
which can be scheduled if you want an automatic deletion once a week or whatever. As Mark points out, one nice feature of this approach is that you can control the deletion on a project by project basis.

How can you edit the description, author, etc. for the built exe in MS visual C# 2008?

I have finished my program in c#, hit build solution, and grabbed the exe out of the bin folder in my project directory. I noticed the description under the filename was "WindowsFormApplication1". I browsed briefly through any fields in the solutions explorer I might change, but nothing worked. Am I doing the right thing to release my program, and/or where can you change that description? I would like to just pass the exe around.
Two methods.
Right click on project=>properties=>Application=>Assembly Information...
Solution Explorer=>Project=>Properties folder=>AssemblyInfo.cs
You can change them in the project properties at Application -> Assembly Information....
If you're concerned about the meta data in general, you may want to disable the pdb at Build -> Advanced... -> Debug Info or the assembly will contain the full path to the .pdb file, wich usually contains the user name.
I'd like to add that in case you're working with multiple projects in one solution, one could create a separate build properties file. This way you don't need to set the properties per project.
Create the file Directory.Build.Props in the solution folder.
Add the following properties
<Project>
<PropertyGroup>
<Version>1.0.0.0<Version>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<FileVersion>1.0.0.0</FileVersion>
<Company>Stack Exchange Inc.</Company>
<Authors>Jeff Atwood, Joel Spolsky</Authors>
<Copyright>user contributions licensed under cc by-sa. rev 2020.10.26.37891</Copyright>
</PropertyGroup>
</Project>

Auto Include Files In A C# Project

This may not really sound hard to do but it is currently killing me. Currently i have a Visual Studio 2008 C# Project that i use in conjunction with a DAL generator , its nothing fancy at all besides generating files that i can use in the project to access the database.
The problem i am having is that after the generator has run and the files are created they never show up in my project (new files , old existing files are fine). To add them i have to show hidden files (In visual studio) then include all of the files manually. So is there anyway to automatically include these files into my project.
Thanks in advance
In VS2008:
1) Right click on your project node.
2) Select "Unload project".
3) Right click on the project node.
4) Select "Edit foo.csproj"
5) In the element add a element that includes your DAL files using wildcards like:
<Compile Include="DAL_*.cs" />
or
<Compile Include="DataAccessLayer\*.cs" />
6) File.Save
7) Right click on the project node.
8) Select "Reload project".
To learn more about this read up on MSBuild.
I suppose the easiest way would be to write a tool to automatically modify the .csproj file, which is just XML, so that it includes your new items.
I'd go one further than mquander and say generate the whole project file for your DAL. When it changes I think VS should prompt you to reload it.
The csproj is an msbuild file and isn't that hard to comprehend.

Categories

Resources