40

Often I need to publish a web project preserving a directory tree even if the directories are empty, for example, directories to hold uploaded files. If I publish my app using the VS2010 publish command, the empty directories are not created on the remote filesystem, on the web server.

Is there a way to force VS to create certain folders, albeit empty, on the target directory? thanks!

pomarc
  • 2,194
  • 3
  • 23
  • 30

5 Answers5

41

No this is not possible I'm afraid we had the same problem. You need to create a placeholder.txt file in each empty directory if you want the precompilation tool to generate these empty folders. Failing that you can create a command line app that will create the folders in your post build events (but only if you are using web application project not web site project).

Hope this helps.

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
Exitos
  • 29,230
  • 38
  • 123
  • 178
8

I know this question mentions Visual Studio 2010 but I got here looking for the same problem...

I'm using Visual Studio 2012 and found a nice/elegant way to overcome this annoying situation. I think it'll also work in VS 2010 and is better than adding dummy empty files inside the empty folders.

When you create a publish profile with VS 2012 it will generate a MyPublishProfile.pubxml file in the Properties\PublishProfiles folder (My Project\PublishProfiles for VB). These are MSBuild files and one can edit them to customize the publish process. In our case we can inject a target into the publish process, before the publish actually occurs like this:

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <AfterAddIisSettingAndFileContentsToSourceManifest>CreateEmptyFolders
    </AfterAddIisSettingAndFileContentsToSourceManifest>
  </PropertyGroup>
  <Target Name="CreateEmptyFolders">
    <Message Text="Adding empty folder to hold Files" />
    <MakeDir Directories="$(_MSDeployDirPath_FullPath)\Files\MyEmptyFolder"/>
  </Target>
</Project>

The property AfterAddIisSettingAndFileContentsToSourceManifest holds the names of targets that will be invoked after the contents for the deployment package has been prepared. This is the perfect time to add additional files or folders to the package.


Code and text mixed from:

How to execute a PowerShell script only before a web deploy Publish task in VS 2012?

Web Deploy : Customizing a deployment package

Community
  • 1
  • 1
Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
  • 1
    Hmm, didn't work for me. I have a feeling I've missed something though. I've pasted your code into the bottom of an existing publish profile and nothing. – jpierson Aug 04 '15 at 18:28
3

There was a bug report to Microsoft about this and they said that they would not fix it. http://connect.microsoft.com/VisualStudio/feedback/details/546356/publish-deploy-does-not-deploy-empty-folders

Too bad. Because it used to work in Visual Studio 2008.

Note that it is not necessary to actually deploy the dummy file. It only needs to exist as part of the project in the build environment.

  • 1
    Looks like the note on this answer is wrong. Setting the build action to None stops the folder from being created on the IIS Server. – GoClimbColorado Feb 26 '13 at 17:49
  • What a annoying bug! Neither 'None' nor 'Compile' helps... So now my file is named "ReadMe.txt" and its content is "This is a folder that blah blah blah" (with 'Content' compile mode). – Chris W Mar 28 '13 at 09:33
  • The note is correct. Set the Build Action to "Content" and the Copy to Output Directory as "Do not copy" – Robert Tanenbaum Mar 07 '14 at 19:05
  • Too bad, looks like the connect link is dead now. – jpierson Aug 04 '15 at 18:19
2

This one worked for me:

Edit your publish pubxml file that visual studio generates, like Leniel Macaferi mentioned, then add this: Visual studio already generated a property like

<publishUrl>f:\publish_web</publishUrl>

in the

<PropertyGroup>    

section, so I've used it like this: Added to section:

 <PipelineDependsOn>
      CustomBeforePublish;
      $(PipelineDependsOn);
    </PipelineDependsOn>  

And also added:

<Target Name="CustomBeforePublish">
    <Message Text="CustomBeforePublish task:"/>
    <MakeDir Directories="$(publishUrl)\MyFolder\MyFolder2"/>
  </Target>
BornToCode
  • 9,495
  • 9
  • 66
  • 83
  • Worked for me in VS 2013. To clarify: add `` to the same `` as `` and add `` after that ``. – Keith Sep 15 '15 at 21:23
1

Inside the Empty Folder Add a Default.aspx file. You can leave it empty, It will then create the folder in the PreCompiled version.

Israel Margulies
  • 8,656
  • 2
  • 30
  • 26