vb.net launch command from terminal - vb.net

I am trying to launch a cmd line from terminal in vb.net;
the command is located in folder C/commands/commandName and takes 2 parameters so that at the end I would write on the terminal this line:
commandName arg1 arg2
I also need to see what the prompt is returning so it has to stay opened
I've tried
Private Sub btnCreateWp_Click(sender As Object, e As EventArgs) Handles btnCreateWp.Click
Dim command As String
Dim path As String
Dim arg1 As String
Dim arg2 As String
path = C:\command\commandName
arg1 = arg1
arg2 = arg2
command= path & " " & arg1 & arg2
Shell("cmd.exe /k" + command)
End Sub
but it doesn't work as expected

Related

Copy files with multiple search criteria

I'm trying to copy files in a directory into a new folder. The script works on one file type, but I have 6 types I need to search for. I thought I could use a bar ("|") like you can with a Regex but that didn't work. Then I tried using an array and had no luck there.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnMove.Click
Dim sourceDir As String
sourceDir = txtMovePath.Text
Dim foundFile As Object = Nothing
Dim graphicsFldr As String
graphicsFldr = sourceDir + "\Graphics\"
For Each foundFile In My.Computer.FileSystem.GetFiles(sourceDir, _
Microsoft.VisualBasic.FileIO.SearchOption.SearchTopLevelOnly, _
"*.cgm|*.eps|*.svg|*.wmf|*.jpg|*.png|*.iso")
My.Computer.FileSystem.CopyFile(foundFile, graphicsFldr & My.Computer.FileSystem.GetName(foundFile))
Next
End Sub
End Class
Module mainModule
Sub Main()
Form1.Show()
End Sub
Use this
Dim arr = {"*cgm","*.eps","*.svg","*.wmf","*.jpg","*.png","*.iso"}
For Each foundFile In My.Computer.FileSystem.GetFiles(sourceDir, _
Microsoft.VisualBasic.FileIO.SearchOption.SearchTopLevelOnly, arr)
My.Computer.FileSystem.CopyFile(foundFile, graphicsFldr & My.Computer.FileSystem.GetName(foundFile))
Next

how to get the argument of ffmpeg process in vb.net?

i am writing a simple program to get the all the properties of a process like ffmpeg . i got most properties of predefined functions but i want to know the argument which i gave to the ffmpeg in vb.net?
For Each prog As Process In Process.GetProcesses
If prog.ProcessName = "ffmpeg" Then
al.Add(prog.Id)
End If
Next
For Each id In al
Dim p As Process = Process.GetProcessById(id)
listBox3.Items.Add(Process.GetProcessById(id).ProcessName)
ListBox3.Items.Add(p.BasePriority)
ListBox3.Items.Add(p.HandleCount)
ListBox3.Items.Add(p.Id)
ListBox3.Items.Add(p.MainWindowTitle)
ListBox3.Items.Add(p.MaxWorkingSet)
ListBox3.Items.Add(p.MinWorkingSet)
ListBox3.Items.Add(p.PriorityBoostEnabled)
ListBox3.Items.Add(p.PriorityClass)
ListBox3.Items.Add(p.PrivilegedProcessorTime)
ListBox3.Items.Add(p.ProcessName)
ListBox3.Items.Add(p.ProcessorAffinity)
ListBox3.Items.Add(p.StartTime)
ListBox3.Items.Add(p.TotalProcessorTime)
ListBox3.Items.Add(p.UserProcessorTime)
lastBox3.Items.Add(p.WaitForInputIdle)
ListBox3.Items.Add("========================")
Next id
Using Jesse Slicer's code as a base from here:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each proc As Process In Process.GetProcessesByName("ffmpeg")
Debug.Print("ID: " & proc.Id)
Debug.Print("Arguments: " & proc.GetCommandLine)
Debug.Print("------------------------------")
Next
End Sub
End Class
Public Module Extensions
<Runtime.CompilerServices.Extension()>
Public Function GetCommandLine(ByVal proc As Process) As String
' Project --> Add Reference --> System.Management
Dim arguments As New System.Text.StringBuilder
Using searcher As New Management.ManagementObjectSearcher("SELECT CommandLine FROM Win32_Process WHERE ProcessId = " & proc.Id)
For Each arg In searcher.Get
arguments.Append(arg("CommandLine") & " ")
Next
End Using
Return arguments.ToString.Trim
End Function
End Module
Note that you need to add the System.Management reference, and I made the function an extension method of the Process class.
ffmpeg is open source, so Might it will be useful:
https://ffmpeg.org/documentation.html

StreamWriter text file gets created but contains no lines

I'm trying to write some text lines to a little log file in a Windows Form application and I cannot see why no lines are written. The file gets created OK and all of the following executes without error but when I open the new file with Notepad, there are no lines. Key snippets follow:
Dim sFileName = App_Path() & "\logs\" & sJobName & ".log"
Try
Using fs As FileStream = New FileStream(sFileName, FileMode.Append, FileAccess.Write)
Using w As StreamWriter = New StreamWriter(fs)
Dim s As String = "Beginning execution (JobName=" & sJobName & ")"
Log(s, w)
s = "Connection in effect: " & BuildConnectString()
Log(s, w)
Dim loader As New Importer
loader.LoadData(Me.txtFN.Text, w)
End Using
End Using
Catch ex As Exception
MsgBox(ex.Message)
End Try
Public Sub Log(logMessage As String, w As StreamWriter)
w.WriteLine("{0} {1}: {2}", DateTime.Now.ToLongTimeString(), _
DateTime.Now.ToShortDateString(), logMessage)
End Sub
and then I'm trying to write to this log from a different class which has been passed the StreamWriter as a parameter:
Public Function LoadData(ByRef filename As String, _
ByRef w As StreamWriter) As String
Dim s As String = "Test logging from loader class"
Mainform.Log(s, w)
In this little test, I am expecting to see 3 lines but I'm getting nothing. I cannot see what I am doing wrong.
It works for me, but if the code doesn't work for you, you can use code like this ...
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim sFileName = App_Path() & "\logs\" & sJobName & ".log"
Try
Dim s As String = "Beginning execution (JobName=" & sJobName & ")"
Log(s, sFileName)
s = "Connection in effect: " & BuildConnectString()
Log(s, sFileName)
Dim loader As New Importer
loader.LoadData(Me.txtFN.Text, sFileName)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Public Sub Log(logMessage As String, StrPath As String)
IO.File.AppendAllText(StrPath, String.Format("{0} {1}: {2}", DateTime.Now.ToLongTimeString(), DateTime.Now.ToShortDateString(), logMessage) + vbCrLf)
End Sub
Public Function LoadData(ByRef filename As String, _
StrPath As String) As String
Dim s As String = "Test logging from loader class"
Log(s, StrPath)
End Function

Create Shortcut in StartUp folder (VB.NET)

I have this code and it's giving troubles:
Imports IWshRuntimeLibrary
Imports Shell32
Public Sub CreateShortcutInStartUp(ByVal Descrip As String)
Dim WshShell As WshShell = New WshShell()
Dim ShortcutPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Startup)
Dim Shortcut As IWshShortcut = CType(WshShell.CreateShortcut(ShortcutPath &
Application.ProductName & ".lnk"), IWshShortcut)
Shortcut.TargetPath = Application.ExecutablePath
Shortcut.WorkingDirectory = Application.StartupPath
Shortcut.Descripcion = Descrip
Shortcut.Save()
End Sub
According to what I have read, this is how you create a shortcut in Startup. But, no matter how much I call this Sub, shortcut does not show up. I ALREADY look up to a lot of similar questions around S.O and various other sites.
I even tried to create the shortcut from other application and still doesn't show up as expected.
What am I missing?
You have two errors in your code:
1) The path isn't being concatenated properly so change this:
Dim Shortcut As IWshShortcut = CType(WshShell.CreateShortcut(ShortcutPath & Application.ProductName & ".lnk"), IWshShortcut)
to this:
Dim Shortcut As IWshShortcut = CType(WshShell.CreateShortcut(System.IO.Path.Combine(ShortcutPath, Application.ProductName) & ".lnk"), IWshShortcut)
2) You spelled Description wrong so change:
Shortcut.Descripcion = Descrip
to this:
Shortcut.Description = Descrip
Here is the fixed subroutine:
Public Sub CreateShortcutInStartUp(ByVal Descrip As String)
Dim WshShell As WshShell = New WshShell()
Dim ShortcutPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Startup)
Dim Shortcut As IWshShortcut = CType(WshShell.CreateShortcut(System.IO.Path.Combine(ShortcutPath, Application.ProductName) & ".lnk"), IWshShortcut)
Shortcut.TargetPath = Application.ExecutablePath
Shortcut.WorkingDirectory = Application.StartupPath
Shortcut.Description = Descrip
Shortcut.Save()
End Sub
Imports Microsoft.Win32 and
Imports IWshRuntimeLibrary
TO CREATE A SHORTCUT
Private Sub btnCreateShortcut_Click_(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateShortcut.Click
'To create Start shortcut in the windows Startup folder:
Dim WshShell As WshShell = New WshShell
Dim SRT As String = Environment.GetFolderPath(Environment.SpecialFolder.Startup)
'Fixing the Shortcut Location instead of StartupScreenLocker
'Name your Shortcut, Example \ScreenLocker.Ink
Dim ShortcutPath As String = SRT & "\ScreenLocker.lnk"
'Add shortcut.
Dim Shortcut As IWshRuntimeLibrary.IWshShortcut
Shortcut = CType(WshShell.CreateShortcut(ShortcutPath), IWshRuntimeLibrary.IWshShortcut)
Shortcut.TargetPath = Application.ExecutablePath
Shortcut.WorkingDirectory = Application.StartupPath
Shortcut.Save()
End Sub
TO DELETE THE SHORTCUT
Private Sub btnDeleteShortcut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteShortcut.Click
'To create Start shortcut in the windows Startup folder:
Dim Shortcut As String = Environment.GetFolderPath(Environment.SpecialFolder.Startup)
'Name the Shortcut you want to delete, Example \ScreenLocker.Ink
Dim ShortcutPath As String = Shortcut & "\ScreenLocker.lnk"
'To delete the shortcut:
If IO.File.Exists(ShortcutPath) Then
IO.File.Delete(ShortcutPath)
End If
End Sub

how to create shortcut on desktop in vb.net without installer

I want to create desktop shortcut for exe file through program.
I do not want to use installer to do this.
Can a piece of code in program do this?How?
Chasler has answered that question a couple of years ago here on SO.
Add a reference to the Windows Script Host Object Model
Imports IWshRuntimeLibrary
Private Sub CreateShortCut(ByVal FileName As String, ByVal Title As String)
Try
Dim WshShell As New WshShell
' short cut files have a .lnk extension
Dim shortCut As IWshRuntimeLibrary.IWshShortcut = DirectCast(WshShell.CreateShortcut(FileName, IWshRuntimeLibrary.IWshShortcut)
' set the shortcut properties
With shortCut
.TargetPath = Application.ExecutablePath
.WindowStyle = 1I
.Description = Title
.WorkingDirectory = Application.StartupPath
' the next line gets the first Icon from the executing program
.IconLocation = Application.ExecutablePath & ", 0"
.Arguments = String.Empty
.Save() ' save the shortcut file
End With
Catch ex As System.Exception
MessageBox.Show("Could not create the shortcut" & Environment.NewLine & ex.Message, g_strAppTitleVersion, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
(Source)
This code works very well
Private Function CreateShortCut(ByVal TargetName As String, ByVal ShortCutPath As String, ByVal ShortCutName As String) As Boolean
Dim oShell As Object
Dim oLink As Object
'you don’t need to import anything in the project reference to create the Shell Object
Try
oShell = CreateObject("WScript.Shell")
oLink = oShell.CreateShortcut(ShortCutPath & "\" & ShortCutName & ".lnk")
oLink.TargetPath = TargetName
oLink.WindowStyle = 1
oLink.Save()
Catch ex As Exception
End Try
End Function
Credits