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
Related
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
Solution1 : { Project1 (windows form), Project2 (class library) }
Trying to copy all .dll(s) I get from after compiling Project1, from the default directory (same as the .exe) to a /lib sub-folder.
if not exist Lib mkdir Lib
for %i in (*.dll) move /Y "$(TargetDir)%i" "$(TargetDir)Lib\%i"
I have problem with the for %i in (*.dll) syntax. What is the correct way of doing it?
Note: This would give no errors (but would copy only 1 .dll, not all):
if not exist Lib mkdir Lib
move /Y "$(TargetDir)first.dll" "$(TargetDir)Lib\first.dll"
You were almost there. You should use a double percentage %% and do:
for %%i in (*.dll) do move /Y "$(TargetDir)%%i" "$(TargetDir)Lib\%%i"
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>
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!!
In visual studio on the post build event command line I have the following:
xcopy $(TargetPath) $(SolutionDir)FunnelWeb.Web\bin\Extensions\ /Y
and its creating the error:
Error 1 The command "xcopy C:\Users\Exitos\Desktop\FunnelWeb-2.0.2.572-source\src\FunnelWeb.Extensions.MetaWeblog\bin\Debug\FunnelWeb.Extensions.MetaWeblog.dll
C:\Users\Exitos\Desktop\FunnelWeb-2.0.2.572-source\src\FunnelWeb.Web\bin\Extensions\
/Y
xcopy C:\Users\Exitos\Desktop\FunnelWeb-2.0.2.572-source\src\FunnelWeb.Extensions.MetaWeblog\bin\Debug\CookComputing.XmlRpcV2.dll
C:\Users\Exitos\Desktop\FunnelWeb-2.0.2.572-source\src\FunnelWeb.Web\bin\Extensions\
/Y" exited with code 4. FunnelWeb.Extensions.MetaWeblog
Im confused as to where the $(TargetPath) and $(SolutionDir) are set, and why this error happened?
xcopy exit codes can be found here: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/xcopy.mspx?mfr=true
I suspect the problem is that you need double quotes around your paths:
xcopy "$(TargetPath)" "$(SolutionDir)FunnelWeb.Web\bin\Extensions" /Y
Becouse of spaces inside your path.
When you open a .sln, $(SolutionDir) is the directory where it came from. If you
want to change it, move the root folder for your .sln file.
Hope it helps.