Echos from Post-Build events are only shown after build is complete - visual-studio

I am using Visual Studio 2017, but the same behavior was found in Visual Studio 2015.
I have several steps in my post-build events and echo messages in between, like so
echo Copying assets...
xCopy "$(TargetDir)..\..\11 Assets" "$(TargetDir)" /i /s /e /y /q
if $(ConfigurationName) == Debug goto :exit
echo Copying compiled files to release folder...
mkdir "$(TargetDir)_ForRelease"
xCopy "$(TargetDir)App.exe" "$(TargetDir)_ForRelease" /y /q
[...]
Now this all works fine, but what I wanted was to see the echo messages appear one by one in the build output window while the post-build is running (so I know at what step in the post-build I am).
Instead, no messages are displayed during the post-build and instead all messages are displayed as one chunk after the build is completed.
I found one similar question, but it talks about the output window locking up completely, which is not the case here. Everything is responsive, the messages are just not displayed immediately.
Is there a way to solve this and display the messages immediately during the post-build, or is this just how the build process works?

What I ended up doing was using MSBuild directly as an external tool from the toolbar. I basically made a "Build Project" button that calls MSBuild.exe on the solution.
MSBuild shows a command window that prints out the post build steps in real time.

Related

How to restart Explorer as a pre-build event in Visual Studio

I'd like to restart Explorer when building my DLL in Visual Studio. I've tried adding taskkill /f /IM explorer.exe and start explorer as pre-build events in the project configuration. When I then build my project, Explorer will in fact be terminated, but it will not be successfully restarted. The taskbar will remain missing. Only after starting explorer from a command prompt will it return. I've also tried executing a call to those commands in a .bat file, but I get the same problem (although the .bat file works properly when executed on its own, outside Visual Studio). I've also tried putting the relaunch part in a post-build event, but no dice. Is there any trick to getting Explorer to relaunch successfully in a pre-build event? Thanks for any input.
You can restart Explorer by putting the following commands into a Batch file:
#echo off
taskkill /f /im explorer.exe
start explorer.exe
exit
(The /f switch will force the termination of Explorer.)
Then, you can execute that Batch file as a pre-build step (Build Events → Pre-Build Event → Command Line).
If this is a necessary part of your build process, I would recommend adding this Batch file to your solution directory, committing it to your version-control system, and using a relative path in your solution properties.

Deleting entire folder in Visual Studio Post-build event

I'm trying to delete a folder in my output directory using the following command line:
del /F "$(TargetDir)Content\"
Tho I always end up exiting with error code 1. I've tried several different ways, without /F, with/without slash both before and after, etc.
Error 1 The command "del /F "E:\proj\bin\Windows\Debug\Content\"" exited with code 1.
There are a lot of questions regarding deleting files in post-build event command lines in visual studio, which works fine, but I can't seem to delete a folder without getting code 1.
Any help is appreciated!
RD /S /Q "Full Path of Folder"
In your case:
RD /S /Q "$(TargetDir)Content\"
Browse to the same folder using Command Prompt, and then run that command and see what the actual error is. Might be permissions or something is in use.

XCOPY and VS 2013 Post-Build Errors

As much as this topic has been talked about, I would think that someone else has run into the same issue as me and found a solution.
This XCOPY Script works in another environment using VS 2012 (with macros) and the same environment using the command line.
XCOPY "C:\Web.Forms\bin\Web.Forms.dll" "C:\inetpub\WebSite\bin" /Y /R
XCOPY "C:\Web.Forms\UserControls\*.ascx" "C:\inetpub\WebSite\usercontrols" /Y /R
It keeps throwing an error of 4 and does not copy within VS 2013. If I change it to "COPY" I get an error of 1. For the purpose of testing and to use it in the command line I stripped it of all Macro's. Actually, I used the [macro] generated directory strings from the error message successfully in the command line.
So why doesn't this work in Visual Studio 2013? VS is running as Administrator.

Post-build event can't be executed

I've converted an old Visual C++ 6.0 project to a new Visual C++ 2010 one. It functions but I have a problem with the post build event which I took from the old project. It registered the target file (an .ocx) on the computer:
copy $(ProjDir)\PDFXChange\dll.Debug\*.* $(TargetDir)
regsvr32 /s /c "$(TargetPath)"
echo regsvr32 exec. time > "$(OutDir)\regsvr32.trg"
In my new solution it doesn't work. I've also tested it in single commands: of the three commands (copy, regsvr32 and echo) only the last one could be executed. What could be my error.
Error:
error MSB3073: The command "copy \PDFXChange\dll.Debug\*.* C:\_tests_\ocx2010\Debug\
regsvr32 /s /c "C:\_tests_\ocx2010\.\Debug\LayoutBox.dll"
echo regsvr32 exec. time > ".\Debug\\regsvr32.trg"
:VCEnd" exited with code 3. C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Microsoft.CppCommon.targets
COPY and REGSVR32 seem not to work.
The problem was in the name of the macro $(ProjDir). In Visual C++ 2010 it's $(ProjectDir)
$(ProjDir) -> $(Proj ect Dir)
Some macros altered their names (after so many years)!
The command copy \PDFXChange\dll.Debug*.* C:tests\ocx2010\Debug should copy some DLLs into the folder where the next command regsvr32 /s /c "C:tests\ocx2010.\Debug\LayoutBox.dll tried to register the target file. It couldn't find any DLLs there, so it quit with the error message.
Mismatch between
Project->Properties->Configuration Properties->General->Output Directory
and
Project->Properties->Configuration->Linker->Output File
I made the former like so...
$(SolutionDir)$(PlatformTarget)$(Configuration)\
and in the linker like so..
$(SolutionDir)$(PlatformTarget)$(Configuration)$(ProjectName).dll
and it solved the problem

Event Build pre & post to stop and restart Windows service

I've been experimenting with using build events to start and stop Windows service that are being built in my project. However for the pre & post builds fail with an error level 255. I've tried catching this with the pre-build with no luck.
Pre-build
if "$(ConfigurationName)" == "Debug"
(
net stop myService
if errorlevel 2
if errorlevel 255
:exit
:exit
)
Post-build
if "$(ConfigurationName)" == "Release"
(
copy $(TargetDir) C:\Media\Bin\$(ProjectName)
if errorlevel 1 BuildEventFailed
:BuildEventFailed
mkdir C:\Media\Bin\$(ProjectName)
copy $(TargetDir) C:\Media\Bin\$(ProjectName)
)
else if "$(ConfigurationName)" == "Debug"
(
net start myService
)
The following weblog by Joel Varty has a solution which I use:
Use Build Events to rebuild a Windows Service without having to manually stop/start it
The only problem is when you do a rebuild. Visual Studio cleans up the files before the pre-build event fires. This then off course fails because the service is still running. But regular builds work great. Hope this helps.
Try using the opening parenthese on the first line of your pre-build code
The conditional statement doesn't require double-qoute ("")
It should be like
if $(ConfigurationName) == Debug (
net stop myService
...
)
This is how I got it to work:
(this solution was a part of an enterprise software where some dll files are reused by another app)
Model is a project which is referenced in Service project and it is built before Service. Which is why we write these codes in Model's Pre-Build Events:
Model Pre-Build Event:
if not exist "$(SolutionDir)UI\bin\Debug\ServiceFolder" mkdir "$(SolutionDir)UI\bin\Debug\ServiceFolder"
net start | find "[Service Name]"
if ERRORLEVEL 0 (
net stop "Service Name"
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe" -u "$(SolutionDir)UI\bin\Debug\ServiceFolder\Service.exe"
)
exit 0
creates a directory in output folder
finds the service by name
stops it
uninstalls it
exit 0 causes the build process to continue if error occurs here
Service Post-Build Event:
xcopy /E /Y "$(ProjectDir)bin\Debug\*" "$(SolutionDir)UI\bin\Debug\ServiceFolder"
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe" "$(SolutionDir)UI\bin\Debug\ServiceFolder\Service.exe"
net start "Service Name"
copy everything needed for the service to a different folder
installs service
starts service
About permissions?
visual studio will ask for elevated permission automatically

Resources