Let me just start this with what the program is trying to accomplish before I start with my problem. I'm working on a program that checks every 5 to 10 (not implemented yet) seconds to see if you VPN has dropped the way I'm doing this having the user type in there IP before they start the VPN and then they start the VPN and it checks to see if it changes. My problem is that when I compare the two strings even if they are the same the program stays they are different.
Imports System.Net
Imports System.IO
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
LbIP.Text = GetIP()
End Sub
Function GetIP() As String
Dim IP As New WebClient
Return IP.DownloadString("http://icanhazip.com/")
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If GetIP() = TextBox1.Text Then
Label1.Text = "VPN DROPPED"
Else
Label1.Text = "Your Good"
End If
End Sub
End Class
DownloadString will return ip address with \n at the end of the string. you need to remove that and compare.
sample work around
If GetIP().Replace("\n","") = TextBox1.Text Then
Label1.Text = "VPN DROPPED"
Else
Label1.Text = "Your Good"
End If
Related
I am trying to communicate with a CBC Autoanaylyser machine which sends data across RS232 serial port. These are the device settings
enter image description here
I am connecting it to com4 port using a serial to usb adapter
ON COM4 port I am using the following VB code to read the data coming through.
Imports System
Imports System.Threading
Imports System.IO.Ports
Imports System.ComponentModel
Public Class ComReadWrite
Dim myPorts As Array
Dim txtline As String
Dim txtchar As String
Dim txtbyte As String
Dim txtexisting As String
Delegate Sub setTextCallBack(ByVal txt As String)
Private Sub ComReadWrite_Load(sender As Object, e As EventArgs) Handles MyBase.Load
myPorts = IO.Ports.SerialPort.GetPortNames()
portnamecombo.Items.AddRange(myPorts)
WriteButton.Enabled = False
CloseButton.Enabled = False
End Sub
Private Sub Start_Click(sender As Object, e As EventArgs) Handles Start.Click
SerialPort1.PortName = portnamecombo.Text
SerialPort1.BaudRate = BaudRateBox.Text
SerialPort1.ReadTimeout = 500
SerialPort1.Parity = Parity.None
SerialPort1.DataBits = 8
SerialPort1.StopBits = StopBits.One
SerialPort1.Open()
Start.Enabled = False
WriteButton.Enabled = True
CloseButton.Enabled = True
End Sub
Private Sub WriteButton_Click(sender As Object, e As EventArgs) Handles WriteButton.Click
SerialPort1.Write(WriteBox.Text)
End Sub
Private Sub CloseButton_Click(sender As Object, e As EventArgs) Handles CloseButton.Click
SerialPort1.Close()
Start.Enabled = True
WriteButton.Enabled = False
CloseButton.Enabled = False
End Sub
Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
RecievedText(SerialPort1.ReadExisting())
End Sub
Private Sub RecievedText(ByVal txt As String)
If Me.ReadBox.InvokeRequired Then
Dim x As New setTextCallBack(AddressOf RecievedText)
Me.Invoke(x, New Object() {(txt)})
Else
Me.ReadBox.Text &= txt
End If
End Sub
Private Sub portnamecombo_SelectedIndexChanged(sender As Object, e As EventArgs) Handles portnamecombo.SelectedIndexChanged
End Sub
Private Sub Label5_Click(sender As Object, e As EventArgs)
End Sub
Private Sub BindingSource1_CurrentChanged(sender As Object, e As EventArgs) Handles BindingSource1.CurrentChanged
End Sub
End Class
On running the program the form only reads weird unreadable characters like OOOOOOOO but not any thing readable as shown in pic
only shows weird characters
The documentation which came with the device has the following pages which seem to be relevant.
page1
page2
We need to communicate the commands in ASCII code. Enq is Chr(5) ack is Chr(6). These were the unreadable characters.
Hey Thanks to the Stack overflow community for the help. I found the solution it is as follows
The machine is supposed to say enq to which host is supposed to reply ack. But when I read the serial port machine seems to be sending an unreadable character.
Well the enq is equal to ASCII code 5 which does not have any character associated with it so it's unreadable character. So if instead of serialport1.readexisting() I write serialport1.readchar() I get that the machine is saying 5. That is machine is actually sending the enq.
Now we need to send the whose ASCII value is 6
If I say serialport1.write('6') it's not get to work.
What will work is serialport1.write(Chr(6))
And using this I got the machine to send the data.
This is probably a question that many of you have already seen a couple of times, but I'm really desperate since every solution i find on the internet doesn't work properly.
I want to create a simple .txt file. My code atm:
Public Class Form3
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If TextBox1.Text = "" Then
MessageBox.Show("Your world needs a name")
Else
Dim file As System.IO.StreamWriter
file = My.Computer.FileSystem.OpenTextFileWriter("c:\test.txt", True)
file.WriteLine("Here is the first string.")
file.Close()
End If
End Sub
End Class
So as you can see, a file should be created when the button is pressed AND when there is text in the textbox.
Is the program not working or am searching the file in the wrong place? Thanks in avance!
Something like this may also be used:
Public Class Form3
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If TextBox1.Text = "" Then
MessageBox.Show("Your world needs a name")
Else
Using file As New StreamWriter("c:\test.txt", True)
file.WriteLine("Here is the first string.")
file.Close()
End Using
End If
End Sub
End Class
Try this code
Public Class Form3
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If TextBox1.Text = "" Then
MessageBox.Show("Your world needs a name")
Else
Dim file As System.IO.StreamWriter
file = My.Computer.FileSystem.OpenTextFileWriter(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "test.txt"), True)
file.WriteLine("Here is the first string.")
file.Close()
End If
End Sub
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
I am new for programming and trying to learn VB.net through youtube tutorials. so I tried to create separate forms for a "login" and "create new account". The login seems to work fine. However, the create account becomes difficult for me to successfully code it. The main issue I have is with the "Insert" code. I created a "createnewaccount" form and connected a database to it. Then I write a code under "btnsave" so that when people create new account it will be saved to the database. This is where I keep getting issues. where it says "UsersPasswordsTableAdapter1.Insert" it underlines it all the time and the program wouldn't work. Here are my codes. Any help would be helpful.
Imports System
Imports System.Data
Imports System.Data.OleDb
Public Class CreateNewAccount
Private Sub CreateNewAccount_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.UsersPasswordsTableAdapter1.Fill(Me.UsersPasswordsDataSet1.UsersPasswords)
End Sub
Private Sub btnSaveNewAccount_Click(sender As Object, e As EventArgs) Handles btnSaveNewAccount.Click
LoginPage.UsersPasswordsTableAdapter1.Insert(Me.TextBox1.Text, Me.TextBox2.Text, Me.TextBox3.Text)
LoginPage.UsersPasswordsTableAdapter1.Fill(LoginPage.UsersPasswordsDataSet1.UsersPasswords)
MsgBox("Your Account has been saved successfully", MsgBoxStyle.Information)
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
End Sub
Private Sub btnBacktoLogin_Click(sender As Object, e As EventArgs) Handles btnBacktoLogin.Click
Me.Hide()
LoginPage.Show()
End Sub
End Class
Is there any way to send text throught PC's without a client and a server program? Just simply send text from a program to another.
Even though you don't want to use clients and servers, this is the easiest way to go.
The server is a server that runs in the command prompt, but runs in the background of your program. The server and client won't be visible in any way.
A simple answer with few lines of codes are TCP Communication. This uses the ip addresses of the both computers and establish a server/client connection.
Every communication needs something that hosts it, to achieve this you code the program to contain the following:
Imports System.IO
Imports System.Net.Sockets
Public Class Form1
Dim listener As New TcpListener(8000)
Dim Client As TcpClient
Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
listener.Stop()
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Timer1.Start()
listener.Start()
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
Dim Data As String = ""
Dim nStart As Integer
Dim nLast As Integer
If listener.Pending = True Then
Client = listener.AcceptTcpClient()
Dim Reader As New StreamReader(Client.GetStream)
While Reader.Peek > -1
Data &= Convert.ToChar(Reader.Read()).ToString
End While
If Not Data = "" Then
msgbox("This is the data recieved: " & Data)
End If
End If
End Sub
End Class
This will open a "TCPListener" on the localhost port 8000. Whenever a client sends data to the listener, the text of the textbox Textbox1 to the data sent.
To send data to the server, use the following code:
Option Explicit On
Imports System.IO
Imports System.Net.Sockets
Public Class Form1
Dim Client As TcpClient
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Try
//Ip to the local or remote, forwarded server. 127.0.0.1 is localhost - the same machine.
Client = New TcpClient("127.0.0.1", 8000)
Dim Writer As New StreamWriter(Client.GetStream())
Writer.Write("Hello World!")
Writer.Flush()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
This will, when Button1 is pressed, try to send the data/string "Hello World!" to the server.
This could be combined into one by having the application set up as following:
Imports System.IO
Imports System.Net.Sockets
Public Class Form1
Dim listener As New TcpListener(8000)
Dim Client As TcpClient
Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
listener.Stop()
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Timer1.Start()
listener.Start()
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
Dim Data As String = ""
Dim nStart As Integer
Dim nLast As Integer
If listener.Pending = True Then
Client = listener.AcceptTcpClient()
Dim Reader As New StreamReader(Client.GetStream)
While Reader.Peek > -1
Data &= Convert.ToChar(Reader.Read()).ToString
End While
If Not Data = "" Then
'Change the string
End If
TextBox1.Text = Data
End If
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Try
//This has to be the address to the remote
Client = New TcpClient("xx.xx.xx.xx", 8000)
Dim Writer As New StreamWriter(Client.GetStream())
Writer.Write(TextBox2.Text)
Writer.Flush()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
To extend this and make it useable in a real application, use a backgroundworker to simply make the server and client run on another thread.
If you don't want to use socket or pipes, i can only think of files, which is more pc to pc than program to program.