Automatically add all files from folder as embedded resource - c#

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.

Related

Visual studio creates unusual files automatically when Press CTRL + S [duplicate]

My company uses a combination of some database tables, a web page front end and an "export" application to handle our string resources in our web sites.
The export application used to work just fine when we used VS2008, but since switching to VS2010 the resources now have a designer.cs file "beneath" them in the solution explorer.
The problem is that the "export" application only generates the .resx files and not the underlying designer.cs files.
So, is there a way to not have those designer.cs files, or alternatively some way to automatically re-generate (or even some command the export application could call to re-generate them)
I had a problem where VS 2010 would not regenerate the Designer.cs files, and couldn't find the solution elsewhere.
I was able to regenerate them though, without going to the command line.
To fix the issue in Visual Studio 2010 I did the following:
Deleted the Designer.cs file
Right clicked on the main resx file
Selected Run Custom Tool
That rebuilt the Designer.cs file.
Hope that might help someone else in the future..
From MSDN we have:
Compiling Resources into Assemblies
When you build your application, Visual Studio invokes the
resgen.exe tool to convert your application resources into an
internal
class called Resources. This class is
contained in the Resources.Designer.cs
file which is nested under the
Resources.resx file in Solution
Explorer. The Resources class
encapsulates all your project
resources into static readonly get
properties as a way of providing
strongly-typed resources at run-time.
When you build through the Visual C#
IDE, all the encapsulated resource
data, including both the resources
that were embedded into the .resx file
and the linked files, is compiled
directly into the application assembly
(the .exe or .dll file). In other
words, the Visual C# IDE always uses
the /resource compiler option. If you
build from the command line, you can
specify the /linkresource compiler
option that will enable you to deploy
resources in a separate file from the
main application assembly. This is an
advanced scenario and is only
necessary in certain rare situations.
If you prefer to automatically generate the *.designer.cs files from *.resx files when building the project, the following approach worked for us and it might work for you as well:
Close your solution
Open as an XML file the project file in which you want to automatically generate the designer files. Note that you need to load it as an XML file. You can't edit these settings through the project property page.
Add a target to the project as follows:
<Target Name="GenerateDesignerFiles">
<Message Text="Deleting old Designer Files..."/>
<Delete Files="#(EmbeddedResource->'%(RootDir)%(Directory)%(Filename).resources')"/>
<Delete Files="#(EmbeddedResource->'%(RootDir)%(Directory)%(Filename).designer.cs')"/>
<Message Text="Generating Designer Files..."/>
<GenerateResource
Sources="#(EmbeddedResource)"
StronglyTypedLanguage="C#"
StronglyTypedClassName="%(Filename)"
StronglyTypedNamespace="#(EmbeddedResource->'%(CustomToolNamespace)')"
StronglyTypedFileName="#(EmbeddedResource->'%(RootDir)%(Directory)%(Filename).designer.cs')"
PublicClass="true"
>
</GenerateResource>
<Message Text="Generating Designer Files complete."/>
</Target>
Locate the target named "BeforeBuild". This target may be commented out (the default).
Modify the "BeforeBuild" target as follows:
<Target Name="BeforeBuild">
<CallTarget Targets="GenerateDesignerFiles"/>
</Target>
This solution is based on all resource files being listed as "EmbeddedResource" within an ItemGroup of the project file, e.g.
<ItemGroup>
<EmbeddedResource Include="Resources\Creditor\Display_Creditor.resx">
<Generator>PublicResXFileCodeGenerator</Generator>
<LastGenOutput>Display_Creditor.Designer.cs</LastGenOutput>
<CustomToolNamespace>Acme.Web.Resources.Creditor</CustomToolNamespace>
</EmbeddedResource>
<EmbeddedResource Include="Resources\InboundEmail\Tooltip_InboundEmailDetails.resx">
<Generator>PublicResXFileCodeGenerator</Generator>
<LastGenOutput>Tooltip_InboundEmailDetails.Designer.cs</LastGenOutput>
<CustomToolNamespace>Acme.Web.Resources.InboundEmail</CustomToolNamespace>
</EmbeddedResource>
<EmbeddedResource Include="Resources\Creditor\Tooltip_CreditorDetails.resx">
<Generator>PublicResXFileCodeGenerator</Generator>
<LastGenOutput>Tooltip_CreditorDetails.Designer.cs</LastGenOutput>
<CustomToolNamespace>Acme.Web.Resources.Creditor</CustomToolNamespace>
</EmbeddedResource>
</ItemGroup>
Disclaimer: This has been tested with Visual Studio 2013 and C# projects. It may or may not work for other projects and/or other versions of Visual Studio.
Try this:
Right click on resx file
Click on properties
Set the properties:
Copy to output Directory : Copy always
Custom tool : PublicResXFileCodeGenerator
Save and build again.
Problem solved.
Following these steps worked for me.
Delete your designer.cs file.
Click on properties
Out put directory - copy always.
Custom tool: PublicResXFileCodeGenerator
Save and build.
Right click on resx and
Click run custom tool.

using relative path on .nuget packages folder in .csproj file

How to use relative path on .nuget packages folder in .csproj file. I've .txt content files and it is copied to my bin directory if I make a build only if I set the property to copy always. By doing that I see a full hardcoded path value (c:\users\usr1234.nuget\packages\packagename\contentfiles\any\newfile.txt) in my .csproj file
how to set the relative path in my .csproj file or is there any work around for this?
thanks in advance
In my case I had to remove some content files from my solution and the output directory. I did it by including the following in my .csproj file:
<ItemGroup>
<None Remove="$(UserProfile)\.nuget\packages\your.package\*\contentFiles\any\somefile.txt" />
</ItemGroup>
$(UserProfile) corresponds to C:\\users\YourUsername
\*\ is
used to be able to exclude files from differnet versions of your
nuget package so you don't need to update this line when you update
your nuget package to a new version
Also I'm not sure this will work outside Windows

Storing files that available to different projects in solution .NET

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.

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

Categories

Resources