Execute .bat file vb.net - 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.

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 " & ".

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!

Related

Command line to kill Adobe Reader process only if it was not already running

I am trying some cmd.exe coding for the first time. I created a registry key, which contains a command called by a custom protocol handler­. What I'm trying to accomplish : Call Adobe Reader which will automatically print a pdf to a given printer. This part already works. I'm using powershell to download the pdf locally from a URL. I also want to kill the Acrobat process once i'm done, but only if it was already opened BEFORE calling my command, so I don't kill the process if the user already has a pdf opened when calling the protocol handler via Chrome.
Excuse the more than ugly formatting, a one-line command is a must in my case, I can't make a batch file or .ps1 file.
cmd /V /C set exists="0" & (tasklist | find "AcroRd32.exe" > nul) & if
(%%ERRORLEVEL%%==0 set exists="1") & set myvar=%1 & call set
myvar=%%myvar:ie:=%% & call set printer=%%myvar:*###=%% & call set
printer=^"%%printer%%^" & call set printer=%%printer:'=%% & call start
powershell.exe -ExecutionPolicy Bypass -Command $url=%%myvar%%; $pdf =
$url.split('#'); md -Force c:\TempPdf "| Out-Null"; (New-Object
System.Net.WebClient).DownloadFile($pdf,'C:/TempPdf/PatientLabel.pdf'); &
timeout /t 2 & call start acrord32.exe /n /t C:/TempPdf/PatientLabel.pdf
%%printer%% & timeout /t 2 & (if !exists!=="1" taskkill /f /im acrord32.exe)
The part which does not work is the tasklist/taskkill. I want to set the %%exists%% variable to 1 if I can find the process running before starting AcroRd32.exe and kill it after printing if %%exists%%=1. I think this might just not be working because of the execution time/parse, but I don't really know how to use /V and !var! anyway. The exists and ERRORLEVEL variables don't seem to expand.
Thanks for the help !

Access VBA Shell command not working

so I'm trying to get a very simple shell command to execute a batch file that's created, but for some reason the shell command doesn't do anything.
The batch file gets created and when I double click on the batch file it executes the script just fine, but if I try to call the shell command in VBA it appears that it tries to open a command window, but immediately closes it after, not producing any errors. This code used to work fine before I reformatted this laptop AND this code works on another system running Server 2012 R2 (I'm running windows 10)
Dim SedjaShell
f = "C:\Temp\_SedjaScript.bat"
Open f For Output As #1
Print #1, Chr(34) & SedjaDir & Chr(34) & " merge --files " & Page1 & " " & Page2 & " " & TOS & " " & Disclosure & " --output " & OutputDir
Print #1, "Pause"
Close #1
SedjaShell = Shell("C:\Temp\_SedjaScript.bat", 1)
Is there something I might be missing?
EDIT
Adding the contents of _SedjaScript.Bat
"C:\Users\User\Desktop\DealTracker\MergeFiles\sejda-console-3.0.4\bin\sejda-console.bat" merge --files C:\Temp\ContractSummary.pdf C:\Temp\Page2.pdf C:\Temp\TOS.pdf C:\Temp\Disclosure.pdf --output "C:\Users\User\Desktop\DealTracker\Completed Contracts\Company-Name-Inc-07-19-2017-Contract.pdf"
Pause
Thanks to Andre I was able to get the script to work inside VBA's Shell command.
By setting the PATH for my JRE to JAVA_HOME I was able to successfully launch the script.
setx JAVA_HOME "C:\Program Files (x86)\Java\jre1.8.0_141"
setx PATH "%PATH%;%JAVA_HOME%\bin";

Close VB.NET app when main VB6 form is closed

I have 2 VB6 application which may launch the same VB.NET application (same exe) via a subMenu. The VB6 code to start this application is a Shell command :
ret = Shell(exe, 1)
My problem is when the user exit the main application (VB6), it won't close the VB.NET application if he opened it.
I tried to use the Taskkill command. It effectively kills the application, but it kills this app for every users on the server !!
I need to do it only for the current user. Can anyone help ?
EDIT : I found some documentation with Taskkill.
It seems that i can precise a user and a domain in the command. I use the following code :
Dim cmdShell As String
quote = Chr$(34)
cmdShell = "taskkill.exe /f /fi " & quote & "USERNAME eq " & _
LCase(connection_User) & quote & " /im " & exeToKill
If someone have a better idea, i'm in :)

How to delete my old program file?

So how can I delete a old file once it's updated? I'm making a program were once it's updated to delete the old version. I don't have a installer so I wouldn't know the location of the file. How can I delete it after it's updated and I don't know were the user put the file that I need to delete? I've searched and everything that shows up you need to have the path.
You could use ClickOnce as suggested, but if you really don't want to use any "external" tools.. this might do the job:
Sorry for my coding, i use this snipet in some of my projects aswell and it works quite good.
This is used in the application that is being updated...
fileupdatepath = "The path to your file which is the updated one you downloaded."
Dim pi As New ProcessStartInfo("cmd.exe", String.Format("/c ping 1.1.1.1 -n 1 -w 15000 >NUL & move /y {0} {1} & start {1}", fileupdatedpath, Application.ExecutablePath)) With {.CreateNoWindow = True, .UseShellExecute = False}
Process.Start(pi)
Application.Exit()
This waits 15s to ensure the current application is closed properly before starting the new one, this is used if the same name and application path needs to be kept, as the move command is being used to replace any files with the same name., if not you can just start the new one, close the current, and delete the current application file...
Note: 'This works if the application path has no special characters like '#' or ' '
In the above answer, you can put quotes around the fileupdatepath and application.executablepath and the code will work even if if there is a space in the file locations. The final code will look like this:
fileupdatepath = "The path to your file which is the updated one you downloaded."
Dim pi As New ProcessStartInfo("cmd.exe", String.Format("/c ping 1.1.1.1 -n 1 -w 15000 >NUL & move /y {0} {1} & start {1}", """" & fileupdatedpath& """", """" & Application.ExecutablePath & """")) With {.CreateNoWindow = True, .UseShellExecute = False}
Process.Start(pi)
Application.Exit()

Possibility of VB.net and VBScript

Is it possible to include in a VB.net 2008 Project a VBScript (test.vbs) and run it if its while the processing necessary? But the main thing is it should be possible to BUILD just one .exe.
If so, can you also receive values / arguments from the VBS file?
Here is an example, although it's pointless, but it is used for unterstanding:
VB.net -> exe is running
the exe runs please_find_the_coputername.vbs
The script please_find_the_coputername.vbs -> obtained the computer name and sends this variable to VB.net
VB.Net displays the computer name via Msgbox().
Note: I know that I can read out the computer name with VB.net but this example is only for understanding my questions.
Edit:
HI #maxedev thank you for your answer.
Wow.. its nice trick.
But I want only to do this VBScript code in VB.net:
Dim strComputer
strComputer = "LP-BKR"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colComputer = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
For Each objComputer in colComputer
Wscript.Echo "Logged-on Domain: " & objComputer.Domain
Wscript.Echo "Logged-on UserName: " & objComputer.UserName
Wscript.Echo "Logged-on ComputerName: " & objComputer.Name
Next
set objWMIService = Nothing
set colComputer = Nothing
I searched the whole day to get the same Value... but didn't find anything. That's why I decide to do that in this way. But if I think, the trick with clipboard is risky. It pushes the still clipboard text away. How can I realize it?
I'm not sure exactly what you're trying to accomplish, but you could write to a text file and then read it through vb.net - or you could do something like this post to use the clipboard to pass info ie :
VBS:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "cmd.exe /c echo hello world | clip", 0, TRUE
VB.NET:
MessageBox.Show(Clipboard.GetText)
--shows "hello world"
One solution would be to add a reference to the MS Script Control:
http://msdn.microsoft.com/en-us/library/aa227400(v=vs.60).aspx
Using that, you can add literally add code (VBScript) with the AddCode() method then run it and get the output back. I have a tiny example here.
Windows automatically provides the information you're looking for in environment variables:
%USERNAME% -> username of the logged in user
%USERDOMAIN% -> WINS name of the domain the user is logged into
%USERDNSDOMAIN% -> FQDN of the domain the user is logged into
%COMPUTERNAME% -> hostname of the computer