65

I have certain folders which I want to keep in the project but not to include it in publishing.

Is that possible?

Kiquenet
  • 14,494
  • 35
  • 148
  • 243
meetpd
  • 9,150
  • 21
  • 71
  • 119
  • 1
    It appears to be possible. Please check out [Web Deployment: Excluding Files and Folders via the Web Application’s Project File](https://blogs.msdn.microsoft.com/webdev/2010/04/22/web-deployment-excluding-files-and-folders-via-the-web-applications-project-file/). – Michael Minton May 24 '11 at 01:24
  • For .NET Core see [ASP.NET Core Publish Exclude Folder (or .json files)](https://stackoverflow.com/a/56450778/1549918) – Chris Halcrow May 20 '21 at 06:00

6 Answers6

35

If it is a web-site project, you may exclude certain folders and/or files as follows (see elements ExcludeFoldersFromDeployment and ExcludeFilesFromDeployment):

<?xml version="1.0" encoding="utf-8"?>
<!--
This file is used by the publish/package process of your Web project. You can customize the behavior of this process
by editing this MSBuild file. In order to learn more about this please visit http://go.microsoft.com/fwlink/?LinkID=208121. 
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <WebPublishMethod>FileSystem</WebPublishMethod>
        <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
        <LastUsedPlatform>Any CPU</LastUsedPlatform>
        <SiteUrlToLaunchAfterPublish />
        <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
        <ExcludeApp_Data>True</ExcludeApp_Data>
        <publishUrl>D:\YAZILIM\Adopen.2015\PreCompiledWeb</publishUrl>
        <DeleteExistingFiles>True</DeleteExistingFiles>
        <PrecompileBeforePublish>True</PrecompileBeforePublish>
        <EnableUpdateable>True</EnableUpdateable>
        <DebugSymbols>False</DebugSymbols>
        <WDPMergeOption>MergeAllOutputsToASingleAssembly</WDPMergeOption>
        <UseMerge>True</UseMerge>
        <SingleAssemblyName>AdoIntranet</SingleAssemblyName>
        <ExcludeFoldersFromDeployment>customobjects;uploads</ExcludeFoldersFromDeployment> 
        <ExcludeFilesFromDeployment>app.config</ExcludeFilesFromDeployment>
    </PropertyGroup>
</Project>
HGMamaci
  • 1,339
  • 12
  • 20
  • 1
    This is a great solution. Another similar solution is to create a *.wpp.targets* file if you need to apply these rules to all publish profiles. – crush May 29 '15 at 14:55
  • Where does this go? I have a .publishproj file but there is a comment in it that says not to modify it. – nasch Oct 10 '16 at 15:02
  • 3
    @nasch publish definition files are stored under .\App_Data\PublishProfiles with the extension .pubxml – HGMamaci Oct 12 '16 at 16:50
  • 1
    @nasch: Solution Explorer > Project > Properties > PublishProfiles > your.pubxml – juFo Dec 01 '16 at 09:25
  • 1
    I think this should be marked as answer because the current one is not valid for excluding whole folder unless doing this for each file within and newly added ones! – A77 Mar 13 '17 at 13:19
  • @A77 thank you, you are right. But it is not heard :-) – HGMamaci Mar 14 '17 at 13:57
  • how can you do that: letting a folder is _included_ for the Debug but they would be _excluded_ for the Release configuration? – holex Jun 06 '17 at 13:55
  • @holex; publish version is Release version. And in above case we do not want to publish some folders. – HGMamaci Dec 30 '17 at 22:00
33

Michael is totally right, through editing the .csproj file you can manually exclude files/folder from being published.

One easier way if you don't want to mess with the .csproj file is to highlight the file(s) inside the VS solution explorer. Under the properties panel, change build to action from 'content' to 'none'.

This way you don't have to unload the project from the solution, load the .csproj and add a line for each new file you add that doesn't need to be published but instead achieve the same with 3 mouse-clicks.

(assuming you've set the 'Only publish files needed to run this application' under the publishing tab)

jpaugh
  • 6,634
  • 4
  • 38
  • 90
jbokkers
  • 952
  • 11
  • 18
  • 15
    For folders, it looks like the simpler way you recommend doesn't work as there is no "Build Action" property for folders on the Properties panel (at least not in VS2010 and VS2012). To exclude entire folders (instead of just individual files) you can use the solution you referred to in Michael's answer. – RSW Sep 19 '13 at 16:56
  • 4
    @RSW, you're correct. The .cproj option specifically overrules any files marked as 'content'. However if you've got no files marked as 'content' in a folder then that folder won't be published. I'd still rather mark n files then overrule those file settings with a hidden/easily forgotten .csproj one-liner - but each their own. – jbokkers Sep 19 '13 at 17:15
  • 2
    _"The .cproj option specifically overrules any files marked as 'content'"_...that's very interesting and important to know...I didn't realize that! For me it's a potential pitfall either way...if I mark the whole folder, I may unintentionally override something inside it. If I do the files individually, I (or someone else) may later add a file and forget to exclude it. I guess like with most double-edged swords, it's best to fully understand the implications so that whatever choice you make is well informed. – RSW Sep 19 '13 at 17:37
10

You can do a 'Find & Replace' in the Web.cspoj file to quickly eliminate a particular folder from the publish/deployment process

Like so;

<Content Include="Uploads/

to

<None Include="Uploads/
Stacker-flow
  • 1,251
  • 3
  • 19
  • 39
5
  • Unload Project,
  • Right click Edit *.csproj,
  • Add this:
<ItemGroup>
<Content Update="Data\CernainFolder\*.*">
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</Content>
</ItemGroup>
  • Reload Project.

This should work nicely

Zilpio
  • 71
  • 1
  • 3
  • i use Data\CernainFolder because i have it into "Data" folder... you have to use your own folder path – Zilpio Feb 04 '19 at 14:48
4

Another way you can do is you can hide folders in windows explorer that are not needed to be get published(not the best solution but works if you have large set of images that still need to be in development box).

DSharper
  • 3,177
  • 9
  • 29
  • 47
  • 4
    This is the only solution if you have a project of type website. http://forums.asp.net/post/1997766.aspx – RobD Sep 13 '12 at 10:39
  • And if you have a batch file doing the publishing, You can use **ATTRIB +H FolderName** just before publishing and **ATTRIB -H FolderName** to set it back just after publishing – mrd3650 Apr 30 '14 at 10:27
1

As the question is tagged with ASP, there is no .proj file to mess with. With VS2015, there is another useful file instead: website.publishproj. And this Asp.Net article on excluding files and folders mentions the .wpp.targets file.

All of these files contain <ItemGroup> elements, that may have elements like <ExcludeFromPackageFolders>. As these seem documented features, just use them and don't feel guilty for hacking or 'messing'. For me, excluding a directory using the simple instructions of that link and the website.publishproj file worked like a charm!

Roland
  • 4,619
  • 7
  • 49
  • 81