myString
myString

Reputation: 47

Execute .bat file vb.net

I need to install my win service. With installUtil it is just few lines of code.

@ECHO OFF

REM The following directory is for .NET 2.0
set DOTNETFX2=%SystemRoot%\Microsoft.NET\Framework\v2.0.50727
set PATH=%PATH%;%DOTNETFX2%

echo Installing MyService...
echo ---------------------------------------------------
InstallUtil /i MyService.exe
echo ---------------------------------------------------
echo Done.
pause

But my thoughts are without creating .bat file and then runing it. Is there any way i can ".execute" those lines of code above without creating .bat file runing it and then deleting it ?.

I will need to dynamically create this code every time because i need to enter the username/password depending what user entered on .net form.

Upvotes: 1

Views: 3213

Answers (2)

Visual Vincent
Visual Vincent

Reputation: 18310

I know it's been a month since you first asked this, but I recently came up with a pretty good solution to this - only using this simple VB.NET code:

Public Sub InstallService(ByVal ServicePath As String)
    Dim InstallUtilPath As String = IO.Path.Combine(System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory(), "installutil.exe")

    Dim InstallUtilProcess As Process = Process.Start(InstallUtilPath, """" & ServicePath & """")
    InstallUtilProcess.WaitForExit()
    'Service is now installed.

    InstallUtilProcess = Process.Start(InstallUtilPath, "/i """ & ServicePath & """")
    InstallUtilProcess.WaitForExit()
    'The second action is now done. Show a MessageBox or something if you'd like.
End Sub
  • The ServicePath parameter is the path to the service that you want to install.
  • The InstallUtilPath variable will be set to the path of the installutil.exe application. It will get the path for the current framework you're running.

    As I'm running .NET Framework 4 the path is C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil.exe

Hope this helps!

Upvotes: 1

Visual Vincent
Visual Vincent

Reputation: 18310

You could start cmd and doing it in one line via it's arguments:

Process.Start("cmd.exe", "/k set DOTNETFX2=%SystemRoot%\Microsoft.NET\Framework\v2.0.50727 & set PATH=%PATH%;%DOTNETFX2% & InstallUtil /i MyService.exe")

And if you want it to show the text you wrote and to "pause" (stay open):

Process.Start("cmd.exe", "/k set DOTNETFX2=%SystemRoot%\Microsoft.NET\Framework\v2.0.50727 & set PATH=%PATH%;%DOTNETFX2% & echo Installing MyService... & echo --------------------------------------------------- & InstallUtil /i MyService.exe & echo --------------------------------------------------- & echo Done. & pause")

Commands are separated by " & ".

Upvotes: 2

Related Questions