How to open command prompt in specific directory with VB.Net - vb.net

I need to open the command prompt programmatically but open it in a specifically directory.
In Notepad++ there is an option to open a file's directory in the command prompt.
Here is what it looks like in Notepad++.
How would you do this?

you need something like this:
Const WorkingDirectory As String = "E:\Shared" ' the directory you want to be on
Dim exePath As String = Environment.SystemDirectory & "\cmd.exe"
Dim StartInfo As New ProcessStartInfo(exePath)
Dim cmdSession As New Process
StartInfo.UseShellExecute = False
StartInfo.WorkingDirectory = WorkingDirectory ' start in this directory
StartInfo.Arguments = "/k" ' let cmd.exe remain running using /k
cmdSession.StartInfo = StartInfo
cmdSession.Start()
Regards, Paul

Related

How can I start a vbs script from VB.Net and pass arguments to the vb script started

I need to execute a command like "cscript OSPP.vbs /dstatus" from a visual basic App. I am using the bellow code but I have not been able to transfer the argument "dstatus" to the OSPP.vbs. How could I do that? Any help
Dim start As New ProcessStartInfo
Dim task As New Process
Dim so As IO.StreamReader
start.FileName = "C:\WINDOWS\system32\cscript.exe"
start.Arguments = """C:\Program Files (x86)\Microsoft Office\Office15\OSPP.vbs /dstatus"""
start.UseShellExecute = False
start.RedirectStandardOutput = True
start.RedirectStandardError = False
task.StartInfo = start
task.Start()
so = task.StandardOutput
task.WaitForExit()
MsgBox(so.ReadToEnd)
Your arguments are not well formed, since the path has white spaces you need to enclose it between double quotes.
Try this:
Dim args as string =
String.Format("""{0}"" ""{1}""",
"C:\Program Files (x86)\Microsoft Office\Office15\OSPP.vbs",
"/dstatus")
start.FileName = "C:\WINDOWS\system32\cscript.exe"
start.Arguments = args

Need to open file through VB.net on another directory

Through my form, I open a .exe which is located in a folder inside debug so:
\bin\Debug\folder\ .exe
The .exe opens and creates 3 different files then closes. It is meant to create them in the same folder as the .exe but instead creates it in the Debug folder when opened through VB.net using Process.Start().
Would anyone have a possible fix for this so I don't have to move the files?
EDIT
(the .exe itself creates the files, some things may be declared outside of this sub)
Private Sub btnRunExe_Click(sender As Object, e As EventArgs) Handles btnRunExe.Click
If AcptEULA.Checked = True Then
Localpath = Application.StartupPath() + "\MCserver" + "\minecraft_server." + txtVersion.Text + ".exe"
Downloadpath = "https://s3.amazonaws.com/Minecraft.Download/versions/" + txtVersion.Text + "/minecraft_server." + txtVersion.Text + ".exe"
LocalpathParent = Application.StartupPath() + "\MCserver"
Try
Dim dirs As String() = Directory.GetFiles(LocalpathParent, "minecraft_server*.exe")
Dim dir As String
For Each dir In dirs
Process.Start(dir)
Next
Catch
'Console.WriteLine("The process failed: {0}", e.ToString())
End Try
ElseIf AcptEULA.Checked = False Then
MsgBox("You must accept the Minecraft EULA before continuing")
End If
End Sub
Process.Start could be used with a ProcessStartInfo instance in which you could set the WorkingDirectory property.
Dim psi As ProcessStartInfo = New ProcessStartInfo()
psi.WorkingDirectory = LocalpathParent
For Each fileName In Directory.EnumerateFiles(LocalpathParent, "minecraft_server*.exe")
psi.FileName = fileName
Process.Start(psi)
Next
Notice that I have changed the Directory.GetFiles with Directory.EnumerateFiles that allows you to process a file while you loop over the folder files without loading all the filenames in memory inside an array. If you still want to use GetFiles then it is
Dim psi As ProcessStartInfo = New ProcessStartInfo()
psi.WorkingDirectory = LocalpathParent
Dim files as String() = Directory.GetFiles(LocalpathParent, "minecraft_server*.exe")
For Each fileName In files
psi.FileName = fileName
Process.Start(psi)
Next
If you look at the ProcessStartInfo documentation on MSDN you could find a lot of other useful properties to fine tune how your program executes.

Invoking the .bat file by passing arguement from vb.net application

From Vb.Net Application, i am calling .bat file by passing SourceFile , DestinationFile. The .Bat file transfers source file to the destination folder. If i call .bat file directly from command prompt by passing arguements, file is getting transfered.
My code fails to transfer the file. I am not able to find the error where the code fails to execute the .bat file.
Dim strBatchFile As String = String.Empty
strBatchFile = AppDomain.CurrentDomain.BaseDirectory
strBatchFile = strBatchFile.Replace("\bin\Debug", "\ShellScript")
strBatchFile = strBatchFile & "callsfxcl.bat"
Dim proc As New System.Diagnostics.Process()
proc.StartInfo.UseShellExecute = False
proc.StartInfo.FileName = strBatchFile
proc.StartInfo.Arguments = String.Format("{0},{1}", strSourceFile, sSFTP)
proc.StartInfo.CreateNoWindow = True
proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden
proc.Start()
Batch files are run by cmd.exe. cmd /c batfile.bat. If you had of set UseShellExecute to true Windows would have done it for you. I suspect you didn't read the error.
From .NET help fror filename property.
The set of file types available to you depends in part on the value of the UseShellExecute property. If UseShellExecute is true, you are able to start any document and perform operations on the file, such as printing, with the Process component. When UseShellExecute is false, you are able to start only executables with the Process component.
Batch files are run by cmd.exe.
cmd /c batfile.bat.
So cmd is your process and /c c:\path\batch.bat is your arguments.

Run already opened process in vb.net

I have a windows form that, on the click of a button, runs a process (MATLAB) and executes a file.
Dim myProcesses() As Process
myProcesses = Process.GetProcessesByName("Matlab")
If myProcesses.Count > 0 Then
'~~~~ what goes here? ~~~~
Else
Dim startInfo As New ProcessStartInfo
startInfo.FileName = "C:\Program Files\MATLAB\R2011b\bin\matlab.exe"
startInfo.WorkingDirectory = MatlabDir 'MatlabDir is defined elsewhere
startInfo.Arguments = "matlab.exe -r test_plot2"
Process.Start(startInfo)
End If
The above code opens MATLAB and executes the script "test_plot2.m" if MATLAB isn't already open. But what do I write in the first IF statement, if MATLAB is already open, and all I want to do is run the file?
Thanks in advance.
Its supposed to be the same. I mean, it doesn't matter if its opened or not, unless the application (Matlab) manages something different, then you have to guess how. Have you tried just using the same code?
Example:
Dim startInfo As New ProcessStartInfo
startInfo.FileName = "notepad.exe"
startInfo.Arguments = "C:\temp\test.txt"
Process.Start(startInfo)
It doesn't matter if you have Notepad already opened or not.

Open Windows Explorer from VB.NET: doesn't open in the right folder

I am trying to locate a file from a program, in VB.NET
Dim exeName As String = "explorer.exe"
Dim params As String = "/e,""c:\test"",/select,""C:\test\a.txt"""
Dim processInfo As New ProcessStartInfo(exeName, params)
Process.Start(processInfo)
It opens the containing directory "c:\" but doesn't go inside to "c:\test", I would like to have the file selected...
Dim filePath As String = "C:\test\a.txt" 'Example file
Process.Start("explorer.exe", "/select," & filePath) 'Starts explorer.exe and selects desired file
You don't need the folder path in there after the /e, try this for your params:
Dim params As String = "/e, /select,""C:\temp\file.txt"""