我正在尝试使用 Windows CMD 制作一个 vb windows 窗体应用程序来 ping IP 地址或网址,但我没有直接使用 cmd,而是试图通过构建 GUI 来简化该过程。它由一个用于输入的 TextBox、一个用于输出的 RichTextbox 和一个运行命令的按钮组成
我每次运行附加代码时遇到的问题似乎一切正常,直到我按下运行按钮,这似乎使应用程序崩溃,导致以下错误
错误信息
System.InvalidCastException
HResult=0x80004002
Message=Conversion from string "1.1.1.1" to type 'Double' is not valid.
Source=microsoft.VisualBasic.Core
StackTrace:
at microsoft.VisualBasic.CompilerServices.Conversions.ToDouble(String Value,NumberFormatInfo NumberFormat)
at microsoft.VisualBasic.CompilerServices.Conversions.ToDouble(String Value)
at WinFormsApp1.Form1.actionNet(String File,String Text)
at WinFormsApp1.Form1.CMDRun_Click(Object sender,EventArgs e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnmouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m,MouseButtons button,Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd,WM msg,IntPtr wparam,IntPtr lparam)
at Interop.User32.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.Interop.Mso.IMsoComponentManager.FPushMessageLoop(UIntPtr dwComponentID,msoloop uReason,Void* pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunmessageLoopInner(msoloop reason,ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunmessageLoop(msoloop reason,ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext context)
at microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationmodel()
at microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at WinFormsApp1.My.MyApplication.Main(String[] Args)
This exception was originally thrown at this call stack:
[External Code]
Inner Exception 1:
FormatException: Input string was not in a correct format.
代码
'''
Function actionNet(ByVal File As String,ByVal Text As String)
Dim Gstart As New Process()
Gstart.StartInfo.FileName = File
Gstart.StartInfo.Arguments = -Text
Gstart.StartInfo.RedirectStandardError = True
Gstart.StartInfo.RedirectStandardOutput = True
Gstart.EnableRaisingEvents = True
Application.DoEvents()
Gstart.StartInfo.CreateNoWindow = True
Gstart.StartInfo.UseShellExecute = False
AddHandler Gstart.ErrordataReceived,AddressOf Gstart_OutputData
AddHandler Gstart.OutputDataReceived,AddressOf Gstart_OutputData
Gstart.Start()
Gstart.BeginErrorReadLine()
Gstart.BeginOutputReadLine()
Return File
End Function
Delegate Sub UpdateTextBoxDelg(ByVal text As String)
Dim myDelegate As UpdateTextBoxDelg = New UpdateTextBoxDelg(AddressOf UpdateTbox)
Public Sub UpdateTbox(ByVal Text As String)
RichTextBox1.AppendText(Text & System.Environment.NewLine)
End Sub
Public Sub Gstart_OutputData(ByVal sender As Object,e As DataReceivedEventArgs)
If Me.InvokeRequired Then
Me.Invoke(myDelegate,e.Data)
Else
UpdateTbox(e.Data)
End If
End Sub
Private Sub CMDRun_Click(sender As Object,e As EventArgs) Handles CMDRun.Click
RichTextBox1.Text = ""
actionNet("ping 1.1.1.1",TextBox1.Text)
RichTextBox1.Focus()
End Sub
'''