What is Content Build Action in Visual Studio?

自古美人都是妖i 提交于 2019-11-28 07:10:10

"Content" means that it is a deployable project item, it signals that the file needs to be copied to the target machine.

Something you can see with a simple console mode project. Project + Add New Item, pick the Bitmap File item template. Its Build Action is automatically set to "Content". Use Project + Properties, Publish tab and click the Application Files button. Note how the bitmap automatically got added to the list of deployed files:

Go back to the Properties window and change its Build Action to None. Click the button again and note how the file is now no longer included.

Also used by installer utilities that integrate with VS for the exact same reason. And a big deal in web applications, they usually have a lot of files that need to be deployed to the web server.

Updated Visual Studio documentation has more meaningful description:

Content - A file marked as Content can be retrieved as a stream by calling Application.GetContentStream. For ASP.NET projects, these files are included as part of the site when it's deployed.


Also after some testing and with a hint from What are the various "Build action" settings in Visual Studio project properties and what do they do?, I've found that the Content build action has this effect in WPF projects (possibly ASP too).

It adds

[assembly: System.Windows.Resources.AssemblyAssociatedContentFileAttribute("filename")]

to WpfApplication1_Content.g.cs. Read about the AssemblyAssociatedContentFileAttribute.

In console or WinForms applications, it does not do anything (neither in source code nor output binary).

Though in the comment, to previously mentioned question, there's a note about effect on deployment:

Also note that Content will be included when using one-click deploy, but None won't even if "copy if newer" is selected.

Possibly this works even for console and WinForms applications (I haven't tried).

I think this is only relevant for C# projects. See the image:

If you set the 'Copy Output Directory' to 'Copy always' or 'Copy if newer' it will copy that file to the output folder when build. What action is performed when you press 'Build (solution)' depends on the 'Build action' property. From what I understand of the Microsoft website it just copies the file if it is a content file like a text, html or other normal file.

What other Build actions do is not known to me, it seems like an advanced option.

The bottom line: it seems a feature that helps you keep the folder structure and files organised each build, so it is always ready for deployment. I hope this gives you an idea of the feature.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!