Storing files that available to different projects in solution .NET - c#

I have one solution with 2 .NET MVC projects and some class library projects. Also I have some xml files. I need to put these xml files in one place and have access to them from these 2 .NET MVC projects. These files need also be published.
Can I create App_Data folder in class library project and put these xml files there and use them? Or App_Data folder just related to .NET MVC projects?
How can I resolve my issue?

If your two MVC projects are host on same sever, you can store these xml files anywhere you want.
By adding a appSetting key to store the physical path of these xml files folder into web.config file for these two MVC project.
Then your program just read these files by get the path from the web.config file.

It depends a bit on how you plan to use your xml files, but if I understand you correctly you just want that the file is published to the output directory, that will mean that it will be accessible to any libraries/projects that are running.
So if you put your xml library in a common project that you say you have then you can edit properties of that xml file and set e.g. Build Action to Content or Copy To Output directory to Always (depending on how you do the publish it would probably behave a bit differently).
Then this file should appear in both bin folders.
Alternatively if this does not work, you can create a shortcut to the same file from both projects. You can do this by clicking "add existing item", then click on a triangle next to Add button and in the menu you can chose add as a link

Step 1. Create shared folder (that does not included into your projects) and copy all shared files there.
Step 2. Change .csproj file and describe pre-build action, that just will copy all needed files from shared folder into your project. Something like this:
<Target Name="CodeAnalysisRuleset" BeforeTargets="PreBuildEvent" Condition="$(ConfigurationName) == Debug">
<Exec Command="xcopy /Y "$(SolutionDir)\SharedCodingRules\codeAnalysis.ruleset" "$(ProjectDir)"" />
</Target>
Step 3. Explicitly include new files into your project (again change you .csproj file)
<ItemGroup>
<Content Remove="stylecop.json" />
</ItemGroup>
<ItemGroup>
<AdditionalFiles Include="stylecop.json" />
</ItemGroup>
After those 3 steps you will be able use shared files (configurations, rullsets etc.) in differnt projects.

Related

Automatically add all files from folder as embedded resource

We wrote small app in C#. It is "installer" that copy files - embedded resources - to some location.
We created one batch file which copies latest versions of these files and build the solution using msbuild.exe.
The problem here is that if anyone want to add another file (or remove existing file) they have to do it manually through Visual Studio.
Is there some way how one can do this automatically?
Example:
I have folder embeddedResources and in there I have files a.txt and b.txt. Both set as Embedded resources. Is there some automatic way, how to add new file c.txt as Embedded resource if I copy it to the folder embeddedResources? Or how to successfully build the solution if I delete the file a.txt?
Just add following ItemGroup into your .csproj:
<ItemGroup>
<EmbeddedResource Include="embeddedResources/*.txt" />
</ItemGroup>
Every file in embeddedResources folder with .txt extension will be automatically set as embedded resource.

Is it possible to copy an entire directory when deploying to Azure?

I'm using Azure and need files to be copied from my project to Azure's approot directory.
For this I go to file-->properties-->build action and set
Build Action: Content
Copy to Output Directory: Copy Always
It works great for files but now I need to upload several directories.
Is it possible to upload an entire directory to Azure's approot?
I'm using .Net 4.5, Visual Studio 2012.
Yes. But as far as I know, you have to edit your .csproj manually to do it. Use a * or ** to recurse, like so:
<ItemGroup>
<None Include="startup\**">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
I usually add a single file manually and then just change the generated line in my .csproj.
In Visual Studio UI folder don't have any property setup instead only files have propertied which is used by compiler and other packaging tools. If selecting each file is an issue, you do can select all the files in specific folder using keyboard shortcut (Shift + _top_file & _bottom_file in folder) and then just use property setup once.

Exclude files from web site deployment with msbuild

I have a web site project that I deploy using msbuild. In the project there are some files and folders that are needed for the build (e.g. the web.config part replacement files) but that I don't want to deploy to the target site.
The best I could think of is a post-build target that removes these files, but I'd like to know if there is a way to have these files not copied to the output folder.
Hi Check this blog post out it saved my day,
I was trying to exclude the un-minified version of the javascripts, and use only the minified version when published (I'm removing large javascripts and chirp.config) its only needed for debug.
just put this on the Project file as stated on the link.
<ItemGroup>
<ExcludeFromPackageFolders Include="Scripts\large">
<FromTarget>Project</FromTarget>
</ExcludeFromPackageFolders>
<ExcludeFromPackageFiles Include="Scripts\mash.js.chirp.config" />
<ExcludeFromPackageFiles Include="Content\mash.js.chirp.config" />
</ItemGroup>
The published site will not include the following:
Scripts\large
mash.js.chirp.config
You can select the files and set their "Build Action" to "ExcludeFromPackageFiles". That way visual studio will edit the csproj xml and you don't have to.
in the properties explorer for the files change the option "copy to output directory to "do not copy"
You can use MSDeploy with Web Publishing Pipeline to exclude files to be included in the package creation.
You can use something like this if you want to exclude for example App_Data folder from the deployed package
<Target Name="ExcludeApp_Data" DependsOnTarget="$(ExcludeApp_DataDependsOn)" Condition="$(ExcludeApp_Data)" >
<ItemGroup>
<ExcludeFromPackageFolders Include="App_Data">
<FromTarget>ExcludeApp_Data</FromTarget>
</ExcludeFromPackageFolders>
</ItemGroup>
</Target>
Somehow editor doesn't display the code properly.
The above gets generated inside the proj file when you configure the Package/Publish web. You can add your own target to get it done.
For example, if you want to exclude Scripts\jquery files from your build, create seperate ExcludeScriptFiles.wpp.targets file as below
<ItemGroup>
<ExcludeFromPackageFolders Include="Internal">
<FromTarget>ExcludeScriptFiles.wpp.targets</FromTarget>
</ExcludeFromPackageFolders>
<ExcludeFromPackageFiles Include="Scripts\jquery.js;xyz.js">
<FromTarget>ExcludeScriptFiles.wpp.targets </FromTarget>
</ExcludeFromPackageFiles>
</ItemGroup>
This is just a simple example to write your own target.
Hope this helps
I'm using Visual Studio 2012 with Jenkins and the only thing that worked for me was changing "Build Action" to "None:"
Internally this sets the XML tag in the PROJECT.csproj file from "Content" to "None:"
<None Include="form.coffee" />
I closed the project then manually edited the file using another editor to exclude all my coffee files en mass.
(All my coffee files are still transcompiled to js files.)

How to use the final {project}.exe.config file when creating a setup project

I have created a console application (blah.exe) with specific app.config's for dev and prod. These are named dev_app.config and prod_app.config. I have hooked up an AfterBuild target in my csproj file* which copies the correct config file to the bin directory as blah.exe.config.
I have also created a setup project for this console app but I have run into a slight issue. It seems that the setup project uses the actual app.config from the project directory as opposed to the final blah.exe.config (located in bin directory).
|--Bin
| |--Debug
| |--Blah.exe.config <--- I want the setup project to use this file
|--app.config <--- Setup project uses this file at the moment
|--dev_app.config
|--prod_app.config
How can I force the setup project to use the final config file generated in the bin folder and not the actual app.config file?
Additional Information:
My current solution involves adding another AfterBuild command which overwrites the actual app.config file. I don't like approach since it forces me to have an additional file that I don't need. Also, having this file has caused me some grief already since I made changes to the app.config file which got overwritten when building. The question is about how to get the setup project use the final config file in the bin folder and NOT how to manage the config or ways to create a config file.
* Adapted from Deploy an app.config based on build configuration
I have been using that exact same scenario but I use the BeforeBuild instead of AfterBuild, and it has always been fine. I have been doing this on both web and windows projects. Below is the code I am using.
<Target Name="BeforeBuild">
<ItemGroup>
<ConfigSourceFiles Include="Web.$(Configuration).config" />
<ConfigDestinationFiles Include="Web.config" />
</ItemGroup>
<Copy SourceFiles="#(ConfigSourceFiles)" DestinationFiles="#(ConfigDestinationFiles)" />
</Target>
Hope this helps.

In a .csproj file, what is <None Include="..."> for?

How is
<None Include="C:\foo.bar" />
different from
<Content Include="C:\foo.bar" />
?
The MSDN article on the build action property explains the differences.
None - The file is not included in the project output group and is not compiled in the build process. An example is a text file that contains documentation, such as a Readme file.
Content - The file is not compiled, but is included in the Content output group. For example, this setting is the default value for an .htm or other kind of Web file.
One difference is how they get published; "None" items don't get included in a publish, "Content" items do; for example, on the "Application Files" dialog on the Publish tab.
I am not 100% sure (I read the MSDN description of Build Action property) but just copying that answer from MSDN to StackOverflow does not answer the question completely for me.
The difference of None and Content only has an effect on Web projects. For a command line project, WinForm project or UnitTest project (in my case) etc. None and Content have no different behavior.
MSDN: "project output group" or "Content output group" only terms used in a Web project, right?
In my situation, my MSBuild file had an ItemGroup for image resources that appeared as follows:
<ItemGroup>
<Content Include="Resources\image001.png" />
<Content Include="Resources\image002.png" />
<Content Include="Resources\image003.png" />
<Content Include="Resources\image004.png" />
<None Include="Resources\image005.png" />
<None Include="Resources\image006.png" />
<None Include="Resources\image007.png" />
</ItemGroup>
While my project was building fine, this left me wondering why I had a mix of Content and None item type elements in my ItemGroup. This MSDN article (for Visual Studio 2010) gave me the guidance I was looking for:
Note that when the resource editor adds an image, it sets Build
Action to None, because the .resx file references the image
file. At build time, the image is pulled into the .resources file
created out of the .resx file. The image can then easily be accessed
by way of the strongly-typed class auto-generated for the .resx file.
Therefore, you should not change this setting to Embedded
Resource, because doing this would include the image two times in
the assembly.
Resolution: With this guidance, using a text editor, I changed the Content item type elements to None.
Also, for an overview of MSBuild items, see this MSDN article.
Content files are not included in a build, but are included in a publish.
None files are not included in a build or publish, unless they are configured that way by you. For instance, a "Copy to Output Directory" setting of "Always" or "Newer", will cause them to be included in both a build and publish.
I have a project that contains no compilable items (it stores html and javascript for jasmine unit tests).
One day my solution (that contained said project) stopped compiling saying "The target "Build" does not exist in the project".
I added an import to bring in the compiler, which worked fine on my machine but failed using msbuild on the build server.
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
I then changed a line from
<None Include="SpecRunner.html" />
to
<Content Include="SpecRunner.html" />
and it worked on the build server as well.
You need None in a template project file to include files you define in the .vstemplate otherwise they are lost in the creation & translation process. They get left behind in the temp folder it uses to build everything and then deleted shortly after.
In my case .Pubxml is one of those files among None list. It's not meant for solution building or as a static file for web project. But to publish the site to Azure, the configurations are present in this.
As per Microsoft article these are the major types we see among .csproj file tags:
None - The file is not included in the project output group and is not
compiled in the build process. An example is a text file that contains
documentation, such as a Readme file.
Compile - The file is compiled into the build output. This setting is
used for code files.
Content - The file is not compiled, but is included in the Content
output group. For example, this setting is the default value for an
.htm or other kind of Web file.
Embedded Resource - This file is embedded in the main project build
output as a DLL or executable. It is typically used for resource
files.

Categories

Resources