VB 2013 - route add cmd elevated - vb.net

After a few days of googeling and trying i thought let met ask it my self.
I am trying to make this happen:
http://gyazo.com/5274568fcb55a0fe042936e375c0b424
The show current routes is working just fine, and displays the text i ask him to show.
But the route adding not so much let me show you the code :)
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim cmdThread As New Threading.Thread(AddressOf CmdAutomate2)
cmdThread.Start()
End Sub
Private Sub CmdAutomate2()
Dim myprocess As New Process
Dim startInfo As New System.Diagnostics.ProcessStartInfo
startInfo.FileName = "cmd"
startInfo.RedirectStandardInput = True
startInfo.RedirectStandardOutput = True
startInfo.UseShellExecute = False
startInfo.CreateNoWindow = True
myprocess.StartInfo = startInfo
myprocess.Start()
Dim sR As System.IO.StreamReader = myprocess.StandardOutput
Dim sW As System.IO.StreamWriter = myprocess.StandardInput
sW.WriteLine("route add" & textbox1.text & " " & textbox2.text)
sW.WriteLine("exit") 'exits command prompt window
results = sR.ReadToEnd 'returns results of the command window
sW.Close()
sR.Close()
Invoke(finished)
End Sub
Now i know you should use different pieces of code here mainly:
startInfo.UseShellExecute = True
instead of:
startInfo.UseShellExecute = False
But that gives me errors with the redirect (which i found to be confimed as not possible somewhere else in this forum)
Now i dont care for it to show the output i have the other button for it.
But i cannot seem to et this to work as i get different errors all the time like cannot reditect ot redirect not started with different combinations of code from all over the web,,
What am i missing here??

To be able to edit the route table for your PC, your app needs to run as administrator. you can add a manifest to your application which declares that you require administrator privileges, which should prompt as required.

Related

Creating my own CMD in vb.net

I'm in need of help with this program which I'm trying to create.
I'm pretty new to programming in general, so please bear with me.
So what I'm trying to create right now, is my own command prompt, and with that said, let me tell you what I want in the command prompt.
As we know from the normal CMD on windows OS systems, we have our input box(or whatever) which we can use to type commands, such as "start google.com", or "ipconfig" etc...
and we have our output, which tells us the results from the commands we typed.
And I wanna create the exact same thing, just with my own UI and some extras which I'm gonna add later.
Here is my issue tho, whenever I type the shell command in my textbox and execute it, it prints out the exception message, so basically it doesn't run the command.
Here is the code:
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
Dim tmp As System.Windows.Forms.KeyPressEventArgs = e
If tmp.KeyChar = ChrW(Keys.Enter) Then
Try
Shell(TextBox1.Text)
RichTextBox1.SelectionColor = Color.Green
RichTextBox1.SelectedText = TextBox1.Text
Catch ex As Exception
RichTextBox1.Clear()
RichTextBox1.SelectionColor = Color.Red
RichTextBox1.SelectedText = ex.Message
End Try
TextBox1.Text = ""
Else
End If
End Sub
I'd really like some examples if possible, since I'm new and I probably wouldn't understand much without examples.
Using a process object instead of a shell is a lot better of an option for having a conversation with cmd. Here's an example of a simple function that you can pass commands to that will return the command's response. This example just does one command/response, but you can use the same process shown below to enter into a dialog with cmd.exe:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MsgBox(ExecuteCommand("ipconfig /all"))
End Sub
Private Function ExecuteCommand(ByVal command As String) As String
Dim response As String = Nothing
Dim p As New System.Diagnostics.Process
Dim psi As New System.Diagnostics.ProcessStartInfo("cmd.exe")
With psi
.UseShellExecute = False
.RedirectStandardInput = True
.RedirectStandardOutput = True
.RedirectStandardError = True
.CreateNoWindow = True
End With
p = Process.Start(psi)
With p
.StandardInput.WriteLine(command)
.StandardInput.Close()
If .StandardError.Peek > 0 Then
response = .StandardError.ReadToEnd
.StandardError.Close()
Else
response = .StandardOutput.ReadToEnd
.StandardOutput.Close()
End If
.Close()
End With
Return response
End Function
And as you'll see if you run the code above, you can certainly use commands like ipconfig

PSTools and VB.net: The system cannot find the file specified

Im trying to write a program, for work, that will be able to tell me if one person is logged into more than one PC.
Im using PStool's PSloggedon cmd.
here is the code Im experimenting with:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim Proc As New System.Diagnostics.Process
Proc.StartInfo = New ProcessStartInfo("psLoggedon")
'right now the textbox will hold a PC ID from a list of PC's in a database.
Proc.StartInfo.Arguments = "-l \\" & TextBox1.Text & ""
Proc.StartInfo.RedirectStandardOutput = True
Proc.StartInfo.UseShellExecute = False
Proc.StartInfo.CreateNoWindow = True
Proc.Start()
MsgBox(Proc.StandardOutput.ReadToEnd)
Proc.Close()
End Sub
but I am getting this eror:
Win32Exception was unhandled:
The system cannot find the file specified
I checked here:
C:\Windows\System32
and made sure the application files were copied there and they were.
can someone help me out and explain to me what I can do to resolve this issue?
p.s. Im using windows 7
use
string filePath = Environment.GetFolderPath(Environment.SpecialFolder.Windows) + "\\sysnative";
32 bit system command

Issue Creating Text File Via Visual Basic (Not writing text)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim FILE_NAME As String = "C:\KVRequest.txt"
Dim aryText(4) As String
aryText(0) = "TextBox4.Text"
aryText(1) = "TextBox5.Text"
aryText(2) = "TextBox6.Text"
aryText(3) = "TextBox7.Text"
aryText(4) = "TextBox8.Text"
Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True)
objWriter.Close()
MsgBox("Text file created in your C drive, attach this file in an email to someone#gmail.com Please check that all of the details are correct before sending.")
End Sub
What I am trying to do is get the text from the text boxes (4 5 6 7 8) to write into a text file. The code I have creates the file, but does not write text into it, can anyone give me a tip on how to get this working?
Thanks!
Edit: Also while I am here, I was trying to get it so button_1.enabled was only true if all of the text boxes had been edited, but I could not think of a practical way to do this, if you could help me with this I would also be very grateful!
Given the code posted above, the reason nothing is being written to the file is because you're not telling it to write anything to the file. You would need to add something like this between the creation of your StreamWriter and where you close the close method on it:
objWriter.WriteLine(TextBox4.Text)
objWriter.WtiteLine(TextBox5.Text)
etc...
Also, the simplest option for only enabling the save button is to create a Control.TextChanged handler for each of your text boxes (or use the one Sub to do it for all of them by adding all their events to the one handler method) and have it do something similar to:
If TextBox4.Text <> "" And TextBox5.Text <> "" And TextBox6.Text <> "" Then
Button1.Enabled = True
Else
Button1.Enabled = False
End If

OpenFileDialog under the hood

This is my first question here because I ended up in dead end.
I'm using ZIP 2 Secure EXE (very good software from Chilkat) to create setup.exe for application. ZIP 2 Secure EXE can be run without GUI with one or more parameters.
The problem is that when I call ZIP 2 Secure EXE (ChilkatZipSE.exe) without using OpenFileDialog form to determine location of ChilkatZipSE.exe, it doesn't run process with System.Diagnostics.Process class. The way I call ChilkatZipSE.exe is "..\ChilkatZipSE.exe -cfg settings.xml". Everything is OK with settings.xml and there is UnlockCode node which is needed for creating setup.exe file. When I use OpenFileDialog ChilkatZipSE.exe creates desired setup.exe and it's working fine.
Bellow is my code that I use:
Private Sub btnStartApp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStartApp.Click
If txtExtAppPath.Text.Length > 0 AndAlso System.IO.File.Exists(txtExtAppPath.Text) Then
Dim myFile As New FileInfo(txtExtAppPath.Text)
txtExtAppLog.Text = StartApplication(myFile.FullName, txtExtParams.Text, chkIsHidden.Checked)
'txtExtAppLog.Text = StartApplication(txtExtAppPath.Text, txtExtParams.Text, chkIsHidden.Checked)
End If
End Sub
Public Function StartApplication(ByVal fileFullPath_ As String, ByVal fileParameter_ As String, ByVal isHidden_ As Boolean) As String
Dim lassie As String = String.Empty
Try
Dim newProcess As New ProcessStartInfo()
newProcess.FileName = fileFullPath_
newProcess.Arguments = fileParameter_
If isHidden_ Then newProcess.WindowStyle = ProcessWindowStyle.Hidden
If System.IO.File.Exists(fileFullPath_) Then
Using startedNewProcess As Process = Process.Start(newProcess)
'startedNewProcess.EnableRaisingEvents = True
startedNewProcess.WaitForExit()
End Using
Else
lassie = "File " + fileFullPath_ + " doesn't exist."
End If
Catch ex As Exception
lassie = ex.Message
End Try
Return lassie
End Function
Thanks, magnumx.
the problem was the given parameter. When using OpenFileDialog it knows where settings.xml is. But when calling "..\ChilkatZipSE.exe -cfg settings.xml" without OpenFileDialog it must be used as "..\ChilkatZipSE.exe -cfg ..\settings.xml"

Writing commands to cmd with Visual Basic

I'm using VirtualBox to attach a usb device. This usb device only works under 32 bit. My host os is Windows 7 64 bit, my Guest Windows 7 32 bit.
I found code to write to the command prompt and read it back out, this has been tested and works very well. But now after I have read and want to write again the command prompt just freezes. I have no idea why it does that... I also tried the command without VB and the driver attach fine.
Any ideas how I can solve this problem?
Public Class Form1
Private Results As String
Private test As Double
Private test2 As String
'The "Delegate" is used to correct the threading issue (Can't update control directly in VB.net 08/10), and invokes the needed text
update.
Private Delegate Sub delUpdate()
Private Finished As New delUpdate(AddressOf UpdateText)
Private Sub UpdateText()
resultsTextBox.Text = Results
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim CMDThread As New Threading.Thread(AddressOf CMDAutomate)
CMDThread.Start()
End Sub
Private Sub CMDAutomate()
Dim myprocess As New Process
Dim StartInfo As New System.Diagnostics.ProcessStartInfo
'Starts the CMD Prompt
StartInfo.FileName = "cmd.exe"
StartInfo.RedirectStandardInput = True
StartInfo.RedirectStandardOutput = True
'Required to redirect
StartInfo.UseShellExecute = False
'Disables the creation of a CMD Prompt outside application.
StartInfo.CreateNoWindow = False
myprocess.StartInfo = StartInfo
myprocess.Start()
Dim SR As System.IO.StreamReader = myprocess.StandardOutput
Dim SW As System.IO.StreamWriter = myprocess.StandardInput
'Runs the command you entered...
'SW.WriteLine(TextBox1.Text)
SW.WriteLine("cd C:\Program Files\Oracle\VirtualBox")
SW.WriteLine("vboxmanage list usbhost")
'Exits CMD Prompt
'SW.WriteLine("exit")
'Displayes the results...
Results = SR.ReadToEnd
'Im reading the string out to get the right Device id
test = InStr(Results, "0x0547", CompareMethod.Text)
test = test - 58
test2 = Mid(Results, test, 36)
'test2 gives 80be0bc1-6f69-4886-868f-c8857bff34c1
'this is the right id, if i try to input it myselves with:
'C:\Program Files\Oracle\VirtualBox>vboxmanage controlvm "test" usbattach
'80be0bc1-6f69-4886-868f-c8857bff34c1
'it works...
SW.WriteLine("vboxmanage controlvm " + Chr(34) + "test" + Chr(34) + "usbattach " + test2)
SW.WriteLine("exit")
Results = SR.ReadToEnd
SW.Close()
SR.Close()
'Invokes Finished delegate, which updates textbox with the results text
Invoke(Finished)
End Sub
End Class
I think ReadToEnd doesn't finish because the Command Window is still open. I would run half of your commands and then read it in after the EXIT command is called. Then you would simply need to parse the result string and run another Command Window process.