1

I want to delete appsettings.json from the publish folder after publishing the web application. I've added a bat file to do the job and call it on AfterTargets="Publish". My bat file gets executed fine, but it seems the published files are copied in the folder after the script has been called. How can I achieve my goal (without using time delay in the bat)?

The publish profile looks like this:

<Project>
  <PropertyGroup>
    <DeleteExistingFiles>true</DeleteExistingFiles>
    <ExcludeApp_Data>false</ExcludeApp_Data>
    <LaunchSiteAfterPublish>true</LaunchSiteAfterPublish>
    <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <PublishProvider>FileSystem</PublishProvider>
    <PublishUrl>C:\Publish\MyWebApp</PublishUrl>
    <WebPublishMethod>FileSystem</WebPublishMethod>
    <_TargetId>Folder</_TargetId>
    <SiteUrlToLaunchAfterPublish />
    <TargetFramework>net9.0</TargetFramework>
    <ProjectGuid>dd151040-6c68-4080-986d-fc920e06a562</ProjectGuid>
    <SelfContained>false</SelfContained>
  </PropertyGroup>
</Project>

I've set the target in MyWebApp.csproj:

  <Target Name="AfterPublishDelSet" AfterTargets="Publish">
    <Exec Command="dset.bat $(PublishUrl)" />
  </Target>

dset.bat changes the directory and tries to delete appsettings.json:

cd %1

IF EXIST appsettings.json (
    del appsettings.json
) ELSE (
    ECHO "appsettings.json not found"
)

And the output seems to indicate that the files are copied in the publish dir from PubTmp\Out after the script is called:

MyWebApp -> C:\MyWebApp\MyWebApp\obj\Release\net9.0\PubTmp\Out\
clearSettings.bat C:\Publish\MyWebApp
"appsettings.json not found"
Web App was published successfully file:///C:/Publish/MyWebApp

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.