1

I've created a C# ASP.Net web API project that requires 2 folders to exist in the remote server in order for the API to run:

  1. "UserFiles"
  2. "UserFiles/PurgeFiles"

Following the advice given in this answer and this answer, I've added this to my API.csproj file:

<Target Name="CreateUserFilesFolder" AfterTargets="AfterPublish">
    <MakeDir Directories="$(ProjectDir)UserFiles" Condition="!Exists('$(ProjectDir)UserFiles')" />
</Target>

To try and create the UserFiles directory, for example, but it doesn't work.

I've tried updating it to Directories="$(PublishDir)UserFiles" and even $(MSBuildProjectDirectory)UserFiles figuring that was the issue, but it's still not creating the empty folder.

What should the lines in my .csproj look like or do I, perhaps, have to define PublishDir or something similar somewhere to get this to work?

John Bustos
  • 19,036
  • 17
  • 89
  • 151
  • 2
    Methinks you need that `` in your `.pubxml` file instead (`.pubxml` files are also MSBuild files, just like `.csproj` files). However I think having the publish-process create the folder is a bit flakey (simply because the VS "Publish Project" tooling loves to completely overwrite any changes I make to it), is there a reason your application can't create the directories in your `Startup` class or `Program.Main` method? – Dai Mar 25 '22 at 01:48
  • I guess there really isn't any reason for not including it in the `Startup` method, this just seemed to be the advice given in multiple solutions I found and I just can't get it to work. As for placing it in the `.pubxml` file, I can try that, but what would the lines to add look like? Would I still need to include the `AfterTargets="AfterPublish"` for example? – John Bustos Mar 25 '22 at 01:53
  • @Dai, I did put it into my `startup` class and it worked a charm! - Thank you... It's not what the other answers recommended, but it definitely did work. Thanks!!! – John Bustos Mar 25 '22 at 02:05

0 Answers0