Vb.net visualbasic与模块
我正在用VisualBasic编写一个应用程序,向用户介绍他们的电脑 所有这些都在一个模块中Vb.net visualbasic与模块,vb.net,Vb.net,我正在用VisualBasic编写一个应用程序,向用户介绍他们的电脑 所有这些都在一个模块中 Imports Microsoft.VisualBasic.Devices Imports System.Management Imports System.Net Imports System.IO Imports System.Windows.Forms Imports System.Deployment.Application Module ComputerSpecModule Publ
Imports Microsoft.VisualBasic.Devices
Imports System.Management
Imports System.Net
Imports System.IO
Imports System.Windows.Forms
Imports System.Deployment.Application
Module ComputerSpecModule
Public Enum infotypes
ProcesserName
VideocardName
VideocardMem
End Enum
Public Function getinfo(ByVal infotype As infotypes) As String
Dim info As New ComputerInfo : Dim value, vganame, vgamem, proc As String
Dim searcher As New Management.ManagementObjectSearcher( _
"root\CIMV2", "Select * FROM Win32_VideoController")
Dim searcher1 As New Management.ManagementObjectSearcher( _
"Select * FROM Win32_Processor")
If infotype = infotypes.ProcesserName Then
For Each queryObject As ManagementObject In searcher1.Get
proc = queryObject.GetPropertyValue("Name").ToString
Next
value = proc
ElseIf infotype = infotypes.VideocardName Then
For Each queryObject As ManagementObject In searcher.Get
vganame = queryObject.GetPropertyValue("Name").ToString
Next
value = vganame
ElseIf infotype = infotypes.VideocardMem Then
For Each queryObject As ManagementObject In searcher.Get
vgamem = queryObject.GetPropertyValue("AdapterRAM").ToString
Next
value = Math.Round((((CDbl(Convert.ToDouble(Val(vgamem))) / 1024)) / 1024), 2) & " MB"
End If
Return value
End Function
Public oAddr As System.Net.IPAddress 'gets the ipv4 add
Public sAddr As String
Public EmailStarterMessage As String = "This message was sent by SpecMee. SpecMee is a light weight application designed to allow the users to find out the specifications of their machines. Please download this application free at http://www.wilson18.com/projects/SpecMee/" + _
Environment.NewLine + _
"" + _
Environment.NewLine + _
"" + _
Environment.NewLine + _
""
'PC SPEC CONTENT
Public ComputerName As String = (My.Computer.Name.ToString)
Public myOS As String = (My.Computer.Info.OSFullName)
Public Processor As String = (getinfo(infotypes.ProcesserName))
Public HDD As String = (Format((My.Computer.FileSystem.Drives.Item(0).TotalSize.ToString / 1024) / 1024 / 1024, "###,###,##0 GB"))
Public RAM As String = (Format((My.Computer.Info.TotalPhysicalMemory / 1024) / 1024 / 1024, "###,###,##0 GB"))
Public VideoCard As String = (getinfo(infotypes.VideocardName))
Public VideoCardMemory As String = (getinfo(infotypes.VideocardMem))
Public Function Resolution() As String
Dim intx As Integer = Screen.PrimaryScreen.Bounds.Width
Dim inty As Integer = Screen.PrimaryScreen.Bounds.Height
Return intx & " x " & inty
End Function
Public Function InternalIPAddress()
With System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName())
oAddr = New System.Net.IPAddress(.AddressList(0).Address)
InternalIPAddress = oAddr.ToString
End With
End Function
Public Function ExternalIPAddress() As String
Dim uri_val As New Uri("http://www.wilson18.com/projects/SpecMee/curip.php")
Dim request As HttpWebRequest = HttpWebRequest.Create(uri_val)
request.Method = WebRequestMethods.Http.Get
Dim response As HttpWebResponse = request.GetResponse()
Dim reader As New StreamReader(response.GetResponseStream())
Dim myip As String = reader.ReadToEnd()
response.Close()
Return myip
End Function
Public EmailContent As String = ("Computer Name: " & ComputerName & Environment.NewLine & "Operating System: " & myOS & Environment.NewLine & "Processor: " & Processor & Environment.NewLine & "Hard Drive Size : " & HDD & Environment.NewLine & "RAM: " & RAM & Environment.NewLine & "Graphics Card: " & VideoCard & Environment.NewLine & "Graphics Onboard Memory: " & VideoCardMemory & Environment.NewLine & "Resolution: " & Resolution() & Environment.NewLine & "Internal IP Address: " & InternalIPAddress() & Environment.NewLine & "External IP Address: " & ExternalIPAddress() & Environment.NewLine)
End Module
我遇到的问题是,如果模块中的某个东西出现故障,例如如果用户的图形卡没有任何板载内存,那么它将出现故障。这将导致其他所有东西也出现故障
我对VisualBasic非常陌生,如果我犯了任何愚蠢的明显错误,请原谅,欢迎提供任何建议
提前感谢。将可能出现故障的部件放入
Try Catch
-语句中
Public VideoCardMemory As String = getinfo(infotypes.VideocardMem)
Public Function getinfo(ByVal infotype As infotypes) As String
Try
...
value = ...
...
Catch
value = "Cannot be accessed!"
End Try
Return value
End Function
可以,但是除了trycatch之外,还有什么方法可以阻止整个杀戮呢?trycatch有什么问题吗
Try Catch
让您在发出可能失败的调用的确切位置捕获异常,从而使您能够100%控制代码。异常发生后,代码执行会继续顺利进行,下一个系统信息可以像什么都没有发生一样收集。下面是我需要尝试解决的问题之一,即Public VideoCardMemory as String=(getinfo(infotypes.VideocardMem)),但我不能,因为它不在函数中。我能解决这个问题吗?当然,如果符合您的需要,您可以将getinfo
的全部代码放在一个Try Catch
中。相应地更改了示例。