Stop And Start Windows Service On Build - c#

We want to Stop our windows Service before we rebuild it, Then Build the new version, then Copy it to a specified folder (due to the configuration changes putting the exe in different bin folders). then Restart the windows service. It Sounds Simple but the main problem we're having is stopping the service doesn't immediately release the file locks. Causing the build to fail ever other time. (once to stop the service and once to actually copy and restart it)
Currently as stated we do have a way that works exactly 50% of the time. It is however annoying to have to build twice every time we make a change to the solution. The current way is build events on the project level.
The Prebuild Looks like this:
cd C:\Windows\Microsoft.NET\Framework\v4.0.30319
net start | find "ServiceHost"
if Not ERRORLEVEL 1 InstallUtil.exe -u
$(ProjectDir)bin\Current\ServiceHost.exe
Exit /b 0
and the PostBuild Looks Like This:
cd $(ProjectDir)bin
DEL Current\* /F /Q /S
xcopy $(Configuration) Current
cd C:\Windows\Microsoft.NET\Framework\v4.0.30319
InstallUtil.exe $(ProjectDir)bin\Current\ServiceHost.exe
net start ServiceHost
Exit /b 0
There is also the problem of initial creation of the "Current" Folder as our source control doesn't keep the Bin folder. we could add a mkdir current command in there to fix this. if there is a better way to update the service every build your advice would be appreciated.

I have a solution that works. its not ideal but it does seem to work 100% of the time
Prebuild:
net stop ServiceHost
:delete
cd $(ProjectDir)
rmdir /s /q $(ProjectDir)bin\Current
echo delete Error Level %ERRORLEVEL%
goto checkDelete
:checkDelete
cd $(ProjectDir)bin\Current
echo check Error Level %ERRORLEVEL%
if %ERRORLEVEL% == 1 (GOTO end) else (GOTO retry)
:retry
echo Prebuild Retry after 5 seconds
powershell -command "Start-Sleep -s 5"
GOTO delete
:end
echo Prebuild Terminate
Exit /b 0
Post Build:
cd $(ProjectDir)bin
:copy
mkdir Current
xcopy $(Configuration) Current
echo PostBuild Error Level %ERRORLEVEL%
if not %ERRORLEVEL% == 0 (GOTO copy)
echo starting service
net start ServiceHost
Exit /b 0

Related

XCopy failing during build but not from command line?

I have a console program that outputs its exe & dlls to a specified directory.
As a post build event I am trying to copy everything in that directory to another directory.
My xcopy command works from command prompt but fails in VS2010? How can this be?
I am testing it by going to the project folder and executing the following in command prompt. (it is the output from VS2010)
In my post-build event:
xcopy "$(OutDir)*.*" "$(TargetDir)..\..\Foo\Bar\" /s /y /i
From command prompt I am executing the following which works.
xcopy "..\..\..\..\MyDir\baz\zip\*.*" "c:\1\2\3\MyDir\baz\zip\..\..\Foo\Bar\" /s /y /i
Sorry about the directory names.
End result should be two directories with the same files in them:
c:\1\2\3\MyDir\baz\zip
c:\1\2\3\MyDir\foo\bar
The target path is relative to the output directory.
When its executed as part of the build it gives an exit code 4
Initialization error occurred. There is not enough memory or disk
space, or you entered an invalid drive name or invalid syntax on the
command line.
Where am I going wrong?
Got it,
I changed the xcopy command in my post build event to:
xcopy "$(TargetDir)*.*" "$(TargetDir)..\..\Foo\Bar\" /s /y /i
The executed result being:
xcopy "c:\1\2\3\MyDir\baz\zip\*.*" "c:\1\2\3\MyDir\baz\zip\..\..\Foo\Bar\" /s /y /i
Which VS2010 much preferred, I guess you can't use a relative path without a base path.
Why you don'y call batch file which will run xcopy for required files source to destination?
call "$(SolutionDir)scripts\copyifnewer.bat"
With copyifnewer.bat looking like this:
IF NOT EXIST <destination> md <destination>
XCOPY /Y <file> <destination>

Change connectionstring in Visual studio whening publishing

I have an asp.net application. I have two databases set up, a test and production. Write now before I push any updates I manually change my conn string to point to production. Is there a way to do this when I publish my application, automatically change to production? Thanks for any help.
You can use web.config transformations for this.
Yeah web.config transformations or I think a better approach is to separate your configs from your web/app.config, so having a folder for each build configuration for example
Config/Debug/connectionStrings.config
Config/Stage/connectionStrings.config
Config/Production/connectionStrings.config
Then your app or web.config would look like
<connectionStrings configSource="bin\connectionStrings.config" />
To get the environment specific config to your bin directory create a post build event which copies the config based on the current build configuration or copy them manually to your bin. This way you don't have to relay on a build to get your desired configuration.
If you really want this automated your post build event could look something like
"$(SolutionDir)CopyConfigs.bat" "$(ProjectDir)" "$(ConfigurationName)" "$(OutDir).."
and the batch file so the copying can be reused between projects:
#echo CopyConfigs.bat :
#echo Coping Config Files...
set projectDir=%1
set configurationName=%2
set outDir=%3
REM Trim Quotes
for /f "useback tokens=*" %%a in ('%1') do set projectDir=%%~a
for /f "useback tokens=*" %%a in ('%2') do set configurationName=%%~a
for /f "useback tokens=*" %%a in ('%3') do set str3=%%~a
#echo Project Directory: %1
#echo ConfigurationName: %2
#echo OutDir: %3
if not exist %1 goto ProjectDirectoryNotFound
REM Copy the configuration files to the projects output directory
xcopy /Y "%projectDir%Configuration\%configurationName%\*.config" "%outDir%"
xcopy /Y "%projectDir%Configuration\*.config" "%outDir%"
#goto END
:ProjectDirectoryNotFound
#echo Project Directory %projectDir% was not found.
#goto END
:END
#echo Coping Config Done

Create Batch file for iexpress

I am trying to use iexpress to run my batch file which will execute the 2 exe & 1 msi files for me. when i try to do it manually, it works.
following is the code in my batch file.
Start /wait %CD%\1.exe /q
Start /wait %CD%\2.exe /q
msiexec.exe /i "%CD%\3.msi"
but this doesn't seem to be working when i create an exe file from iexpress.
Reference
Above mentioned article has some code(to copy files to temp folder) & but i am not able to understand the syntax.
MKDIR %Tmp%\<UNIQUE PRODUCT NAME>
XCOPY . %Tmp%\<UNIQUE PRODUCT NAME> /S /E /Y
%Tmp%\<UNIQUE PRODUCT NAME>\setup.exe
The problem is that, as you can see from your screenshot, the batch file is being executed by command.com, not cmd.exe. (If you don't specify an interpreter, IExpress uses command.com. Ouch.) So there are no variables like %cd% or %~dp0.
You likely don't need them anyhow. But you do need to execute your batch file explicitly in IExpress like:
cmd.exe /c file.bat
so that it uses a modern command interpreter.
The second bit of code in your question makes the files persistent (ie they won't be deleted after the IExpress archive terminates) by xcopying them to a different directory.
Here is what it means:
1) Creates a directory(MKDIR) with name of "UNIQUE PRODUCT NAME" in the path stored in %TMP% Environment Variable, which normally points to: C:\DOCUME~1\yourusername\LOCALS~1\Temp
MKDIR %Tmp%\<UNIQUE PRODUCT NAME>
2) Then copy recursively all installation files from current folder into the new folder created before.
XCOPY arguments:
/S Copies directories and subdirectories except empty ones.
/E Copies directories and subdirectories, including empty ones.
Same as /S /E. May be used to modify /T.
/Y Suppresses prompting to confirm you want to overwrite an
existing destination file.
XCOPY . %Tmp%\<UNIQUE PRODUCT NAME> /S /E /Y
3) Finally execute the application from the new location
%Tmp%\\setup.exe
Hope this helps
Try replacing %CD% with %~dp0
Assuming that 1.exe is in the same folder as your batch script.
Your %CD% is not working. Please be sure that CMD extensions are enabled (type CMD /x to enable and CMD /y to disable) then expand the %CD% with this code
SET CURDIR=%CD%
Start /wait "%CURDIR%\1.exe" /q
Start /wait "%CURDIR%\2.exe" /q
msiexec.exe /i "%CURDIR%\3.msi"
And I'm not sure that you can start an exe from that location (APPDATA) for security reasons.
Thanks a lot for this forum discussion.Finally i could able to compile all msi files and executables in an one .exe file.
Complete procedure as follows create a batch file
echo on
SET CURDIR=%CD%
msiexec.exe /i "%CURDIR%\1.msi"
"%CURDIR%\3.EXE"
"%CURDIR%\setup.exe"
echo off
You can arrange any number of exe files or msi files as you wish and save the batch file as yourfile.bat.
Now the tricky part is before you proceed to Iexpress, convert the batch file to exe with the software provided by http://www.f2ko.de/programs.php?pid=b2e
Now when you run the program keep the 'Invisible Application' checked to hide the command prompt.You can also encrypt your exe with the password.'Delete at Exit' is optional as the temporary folder will be automatically deleted when the execution of files completed.
Once you successfully compile the batch file,execute the .exe file created.
Bingo!! you'll not see the command prompt window and your applications start executing sequentially.
Begin your Iexpress tool and Add all your files present in the batch file(except batch file).On the ‘Install Program to Launch’ screen, leave the Post Install Command blank and find the following in the Install Program dropdown:'demo.exe'and proceed further to create your complete bunch of single package. Cheers!!

Delete self while running

Im currently creating a uninstallation application which deletes the application folder. The problem is that i cant delete the uninstaller application which is in the same folder cause its running.
Is there a way to delete the application while running, so its just in the memory.
string Installation = UninstallRegister.Read("InstallationLocation");
if (Directory.Exists(StartMenu))
{
Directory.Delete(StartMenu, true);
}
Best regards
A simple idea would be to copy the uninstall application to %TEMP% and run from there. This would however leave a file in %TEMP% (which would probably not be noticed by anyone).
You can use command prompt Del command
nircmd has a command named cmdwait
you can set a wait time to delete your file then close the application and let it delete
You can use this command. It close your app and start cmd remove directory command arter deley=3000
This solution remove app and folder.
Process.Start("cmd.exe", "/C ping 1.1.1.1 -n 1 -w 3000 > Nul & RD /s /q " + Path.GetDirectoryName(Application.ExecutablePath));
Application.Exit();

Xpath error due to miscorrected paths

When i start to debug application i am getting this error Can any one give me the solution
Error 1 The command "xcopy "F:\asoadmin_ML_view\leaf\Console\Bin\Debug\Console.*" "F:\leafPRODUCT\Bin" /f /e /y /i
xcopy "F:\leafPRODUCT..\voyagerhtml\Operations Sentinel Console Help*.chm" "F:\leafPRODUCT\Help" /f /e /y /i
" exited with code 4.
According to this, error code 4 means either you do not have enough memory to perform debugging, or (much more likely) you have invalid syntax. The answer to this thread suggests placing double quotes around your entire path.
Your build process likely has a pre/post build step associated with it that is executing the XCopy commands. Error code 4 implies either insufficient disk space to perform the copy, or a syntax error in the command.
To edit your project's pre/post build steps, right click on your project, then select Properties -> Build Events. To diagnose these commands, try running them from the command line after you build your project.
Maybe the user with which you try to execute the 'xcopy' doesn't have enough permissions (according to this thread: http://social.msdn.microsoft.com/Forums/en-US/tfsbuild/thread/5c4a55e2-243a-427f-800d-39b42c9e860e).

Categories