My Visual Basic opens a million command prompts when I only want one that has all the coding going through it.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles OnButton.Click
Dim textbox123 As String = TextBoxName.Text
Dim Textbox12345 As String = TextBoxPassword.Text
Process.Start("cmd.exe")
SendKeys.Send("netsh wlan set hostednetwork mode=allow ssid=")
SendKeys.Send(textbox123)
SendKeys.Send("passord=")
SendKeys.Send(Textbox12345)
SendKeys.Send("~")
SendKeys.Send("exit")
SendKeys.Send("~")
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs)
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles OffButton.Click
Process.Start("cmd")
SendKeys.Send("netsh wlan stop hostednetwork")
SendKeys.Send("~")
SendKeys.Send("exit")
SendKeys.Send("~")
End Sub
Private Sub TextBox1_TextChanged_1(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub TextBoxName_TextChanged(sender As Object, e As EventArgs) Handles TextBoxName.TextChanged
End Sub
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs)
End Sub
Private Sub TextBox2_TextChanged_1(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
End Sub
Private Sub TextBox3_TextChanged(sender As Object, e As EventArgs) Handles TextBox3.TextChanged
End Sub
Private Sub TextBox4_TextChanged(sender As Object, e As EventArgs) Handles Code1.TextChanged
End Sub
Private Sub Startbutton_Click(sender As Object, e As EventArgs) Handles Startbutton.Click
Dim process As New Process()
process.StartInfo.FileName = "cmd.exe "
process.StartInfo.Verb = "runas"
process.StartInfo.UseShellExecute = True
process.Start("cmd")
SendKeys.Send("netsh wlan start hostednetwork")
SendKeys.Send("~")
SendKeys.Send("exit")
SendKeys.Send("~")
End Sub
End Class
I'm trying to make a hotspot app for a friend. Don't say use a bat file or something just help
Oh and i want to make it so you can change the name of the ssid and password then click start so it starts. however as i said it runs a million cmds and for some reason it doenst run as admin despite me trying to get it to. ( you can change the name and password in mine its just that it runs a million cmd's and never in admin)
So is there anything you can suggest doing? sorry im not very good with VB plus ive updated it.
it works sometimes but other times it doesnt.
so i have process.startinfo("C:\WINDOWS\system32\cmd.exe")
However i have an error stating that "property access must assign to the property or use its value.
Help I know i have asked this question and know I've updated my code.
Public Class Hotspot
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles OnButton.Click
Dim textbox123 As String = TextBoxName.Text
Dim Textbox12345 As String = TextBoxPassword.Text
Dim code123 As String = Code1.Text
Shell("cmd.exe /k cd \temp", AppWinStyle.NormalFocus)
SendKeys.Send(code123 & textbox123 & " key=" & Textbox12345)
SendKeys.Send("~")
End Sub
Shell("cmd.exe /k cd \temp", AppWinStyle.NormalFocus) is supposed to only perform the code in one cmd and not open new ones but surprise surprise it doesn't.
That's one line of 3 codes (one for each button ) for 3 buttons I have which are used to set the hosted network, run the hosted network and stop the hosted network.
Basically my app makes a lan hotspot by reading the text in textboxname as the ssid and textboxpassword as the key.
However when I attempt to run my app ( which works btw) it opens a million different command prompts but I only need one.
Your startinfo isn't being used because you are passing a sting to ProcessStart instead of a StartInfo object. So all that RunAs is unused. I don't know VB.NET but I know how to look up documentation.
So it won't do what you want.
Multiple windows is usually because you have a batchfile called cmd starting cmd somewhere. That's because you haven't specified the extension.
I have since found that my command windows are requesting admin rights.
This Referance :
ForMore
Put This code in Button
Dim process As System.Diagnostics.Process = Nothing
Dim processStartInfo As System.Diagnostics.ProcessStartInfo
processStartInfo = New System.Diagnostics.ProcessStartInfo
processStartInfo.FileName = "cmd.exe"
processStartInfo.Verb = "runas"
processStartInfo.Arguments = ""
processStartInfo.Arguments = "/k netsh wlan set hostednetwork mode = allow ssid=MyNetworkID key=12345678 keyUsage=persistent"
processStartInfo.Arguments = "/k netsh wlan start hostednetwork"
processStartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal
processStartInfo.UseShellExecute = True
Try
process = System.Diagnostics.Process.Start(processStartInfo)
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
If Not (process Is Nothing) Then
process.Dispose()
End If
End Try
End Sub
change the /k to a /c to hide the cmd prompt window
Related
If I use the code down below to redirect standardinput and standardoutput to a textbox, everything is working, except for lines which require interactive input from the user. For instance, if you execute the command dir /p c:\windows, the whole content of the directory is displayed. The /p is not being honored. Same thing if you execute a script which requires user input. Here is a small example:
#echo off
set /p "id=Enter ID: "
Echo The user ID entered is: %id%
When you execute this in a normal cmd window, the output is like this:
Enter ID: MyUser
The user ID entered is: MyUser
But when I execute the same script through my form with redirected StandardInput and StandardOutput, the result looks like this:
Enter ID: The user ID entered is:
So here, the /p to prompt for input is also ignored.
Is there any way to make this work the same way as in a standard cmd.exe windows?
Thanks for any help in advance!
Kind Regards,
Eric
Public Class Form1
Dim WithEvents P As New Process
Dim SW As System.IO.StreamWriter
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
P.EnableRaisingEvents = True
Me.Text = "My title"
AddHandler P.OutputDataReceived, AddressOf DisplayOutput
P.StartInfo.CreateNoWindow() = True
P.StartInfo.UseShellExecute = False
P.StartInfo.RedirectStandardInput = True
P.StartInfo.RedirectStandardOutput = True
P.StartInfo.FileName = "cmd.exe"
P.StartInfo.Arguments = ""
P.Start()
P.SynchronizingObject = Me
P.BeginOutputReadLine()
SW = P.StandardInput
SW.WriteLine()
End Sub
Private Sub DisplayOutput(ByVal sendingProcess As Object, ByVal output As DataReceivedEventArgs)
TextBox1.AppendText(output.Data() & vbCrLf)
End Sub
Private Sub Textbox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
If e.KeyChar = Chr(Keys.Return) Then
SW.WriteLine(TextBox2.Text)
End If
End Sub
Private Sub myProcess_Exited(ByVal sender As Object, ByVal e As System.EventArgs) Handles P.Exited
Me.Close()
End Sub End Class
Drag&Drop is working correctly when application is running. But when a file is dropped on application shortcut or exe.file, no drag&drop event is triggered just aaplication starts.
I created simple application in Visual Studio 2019, only Form1 with following adjustments
Form1.AllowDrop = True
Private Sub Form1_DragEnter(sender As Object, e As DragEventArgs) Handles MyBase.DragEnter
If (e.Data.GetDataPresent(DataFormats.FileDrop)) Then
e.Effect = DragDropEffects.Copy
End If
End Sub
Private Sub Form1_DragDrop(sender As Object, e As DragEventArgs) Handles MyBase.DragDrop
Dim files() As String = CType(e.Data.GetData(DataFormats.FileDrop), String())
Me.Text = files(0)
End Sub
Can you help me how to open my application with correct filename which is dropped on application icon/shortcut?
Thanks, Martin
Your code only handles the drag and drop on the form itself (which can be done with any other control with AllowDrop = True). Dropping a file onto the application executable file (or shortcut) is a totally different thing; what it does is simply open the application normally but with a command-line argument passed to it (i.e., the file/folder path).
To retrieve that file/folder path, you can use Environment.GetCommandLineArgs, to read the command-line arguments, make sure it returns at least two elements (the first one is your application's executable path), and then display the second (or second to last) elements.
This should work:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim args = Environment.GetCommandLineArgs()
If args.Length > 1 Then Me.Text = args(1)
End Sub
If you drop multiple files onto your program's icon and you want to display them all, you can adjust the above code to something like this:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim args = Environment.GetCommandLineArgs()
For Each arg In args.Skip(1)
' Do something with `arg`.
Next
End Sub
So I am working on a form which need to be printed. I want to end up with a PDF file using Foxit PDF printer. The problem is that I cant figure out how to get the selected path as file location so I keep getting an the Path cannot be null.
error. Where in the code should I put my filelocation when using the Printform?
In this code the foldername is the location where I want to print.
Private Sub BtnPrint_Click(sender As Object, e As EventArgs) Handles BtnPrint.Click
Dim folderDlg As New FolderBrowserDialog
Dim foldername As String
folderDlg.ShowNewFolderButton = True
If (folderDlg.ShowDialog() = DialogResult.OK) Then
foldername = folderDlg.SelectedPath
Dim root As Environment.SpecialFolder = folderDlg.RootFolder
End If
PrintForm1.Print()
End Sub
Edit:
Actually deleted part of the code and still getting the same error (first part wasnt doing anything to start with I know that). All I am using now is:
Private Sub BtnPrint_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles BtnPrint.Click
PrintForm1.Print()
End Sub
Also microsoft help database about Printform isnt helping since I have done exactly what it says and still getting the Path is Null error
Edit 2:
So I am using this code now and it is working.
Private Sub BtnPrint_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles BtnPrint.Click
PrintDialog1.PrinterSettings = PrintForm1.PrinterSettings
PrintDialog1.AllowSomePages = True
If PrintDialog1.ShowDialog = DialogResult.OK Then PrintForm1.PrinterSettings = PrintDialog1.PrinterSettings
With Me.PrintForm1
.PrintAction = Printing.PrintAction.PrintToPreview
Dim MyMargins As New Margins
With MyMargins
.Left = 10
.Right = 10
.Top = 10
.Bottom = 10
End With
.PrinterSettings.DefaultPageSettings.Margins = MyMargins
.Print()
End With
End Sub
but as soon as I try to set which area it should print I get the following error: "Printing is not a member of powerpacks". I tried using the following code according to microsoft this is the way it should work.. I have no clue where the error comes from
.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.ClientAreaOnly)
You don't need a path to use printform. Printform just prints what you see on the screen to your default printer. You need to install "Visual Basic PowerPacks" to use this command. More explanation you may find here:
https://learn.microsoft.com/en-us/dotnet/visual-basic/developing-apps/printing/how-to-print-a-form-by-using-the-printform-component
To preview your print you don't need to use printdialog and all this. You just click on printform1 in the designer,to get up the properties window of printform1. In printaction you choose PrintToPreview. Thats all its needed.
These are all the lines I need:
Public Class Form1
Private Sub Exit_Click(sender As Object, e As EventArgs) Handles Button2.Click
Application.Exit()
End Sub
Private Sub Print_Click(sender As Object, e As EventArgs) Handles Button1.Click
PrintForm1.Print()
End Sub
End Class
I am trying to make a simple chat program in vb using netcat. Here is my code
Public Class Form1
Dim p As New Process
Dim pstrt As New System.Diagnostics.ProcessStartInfo
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Go()
End Sub
Sub Go()
pstrt.FileName = "cmd.exe"
pstrt.Arguments = "/c nc -l -p1234"
pstrt.UseShellExecute = False
pstrt.RedirectStandardOutput = True
pstrt.RedirectStandardInput = True
AddHandler p.OutputDataReceived, AddressOf yo
p.StartInfo = pstrt
p.Start()
p.BeginOutputReadLine()
End Sub
Sub yo(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
UpdateTextBox(e.Data)
End Sub
Private Delegate Sub UpdateTextBoxDelegate(ByVal Text As String)
Private Sub UpdateTextBox(ByVal tot As String)
If Me.InvokeRequired Then
Dim delgt As New UpdateTextBoxDelegate(AddressOf UpdateTextBox)
Dim args As Object() = {tot}
Me.Invoke(delgt, args)
Else
RichTextBox1.Text &= tot & Environment.NewLine
End If
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
'Shell(TextBox1.Text)
'Console.WriteLine(TextBox1.Text)
p.StandardInput.WriteLine(TextBox1.Text)
End Sub
End Class
The form has a richtextbox, a textbox and two buttons.
The problem is that:
when using redirectstandardinput, the received text is not displayed in richtextbox but the sent text can be viewed by the receiver.
when I don't redirectstandardinput then the received text is displayed in the richtextbox but the sent text cannot be viewed by the receiver.
I have also tried to use the commented code (for button2 click code) to use to send text when not using redirectstandardinput.
For me, your code is working correctly without changes, with
pstrt.RedirectStandardOutput = True
pstrt.RedirectStandardInput = True
See the screenshot:
I guess your problem can be with netcat.
Try with simple commands first like seen on the sample.
Try on another machine. I tested on Windows 8.0, .NET 4.0.
really need help! I would like to know how to get jar output into a textbox in VB 2008.
Also i would like to send commands to it (like CMD would when you use this command:
C:\Windows\System32\java.exe -Xms128M -Xmx1024M -jar Craftbukkit.jar)
Below a proof of concept. You'll have to tweak it to your own desires and wishes. What does this do:
Start a process in the background
Start reading the output and error streams.
You can send commands via a textbox (and a click on a button).
Write the output/errors in an output textbox.
Again, this code is just a proof of concept, it is far from finished (but it demonstrates enough). You'll have to add some extra checks etc to make it "waterproof".
Public Class MyForm
Private WithEvents _CmdProcess As Process
Private Delegate Sub DisplayTextDelegate(text As String)
Private Sub MyForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim processInfo As New ProcessStartInfo()
processInfo.FileName = "cmd"
processInfo.RedirectStandardError = True
processInfo.RedirectStandardInput = True
processInfo.RedirectStandardOutput = True
processInfo.UseShellExecute = False
processInfo.CreateNoWindow = True
_CmdProcess = Process.Start(processInfo)
_CmdProcess.BeginOutputReadLine()
_CmdProcess.BeginErrorReadLine()
End Sub
Private Sub MyForm_Disposed(sender As System.Object, e As System.EventArgs) Handles MyBase.Disposed
If _CmdProcess IsNot Nothing Then
_CmdProcess.Close()
End If
_CmdProcess = Nothing
End Sub
Private Sub btnExecute_Click(sender As System.Object, e As System.EventArgs) Handles btnExecute.Click
If Not String.IsNullOrWhiteSpace(txtCommand.Text) Then
Dim inputStream As System.IO.StreamWriter = _CmdProcess.StandardInput
inputStream.WriteLine(txtCommand.Text)
inputStream.Flush()
End If
End Sub
Private Sub CmdProcess_ErrorDataReceived(sender As System.Object, e As System.Diagnostics.DataReceivedEventArgs) Handles _CmdProcess.ErrorDataReceived
Invoke(New DisplayTextDelegate(AddressOf DisplayText), Environment.NewLine)
Invoke(New DisplayTextDelegate(AddressOf DisplayText), "Error!")
Invoke(New DisplayTextDelegate(AddressOf DisplayText), e.Data)
End Sub
Private Sub CmdProcess_OutputDataReceived(sender As System.Object, e As System.Diagnostics.DataReceivedEventArgs) Handles _CmdProcess.OutputDataReceived
Invoke(New DisplayTextDelegate(AddressOf DisplayText), e.Data)
End Sub
Private Sub DisplayText(text As String)
txtOutput.AppendText(Environment.NewLine)
txtOutput.AppendText(text)
End Sub
End Class