视觉基础和模块 [英] Visual basic and modules
问题描述
我正在用visual basic编写一个应用程序来告诉用户他们的电脑.
所有这些都在一个模块中
导入 Microsoft.VisualBasic.Devices进口系统.管理导入 System.Net进口系统.IO导入 System.Windows.Forms导入 System.Deployment.Application模块 ComputerSpecModule公共枚举信息类型处理器名称显卡名称显卡内存结束枚举公共函数 getinfo(ByVal infotype As infotypes) As StringDim info As New ComputerInfo : Dim value, vganame, vgamem, proc As StringDim searcher As New Management.ManagementObjectSearcher(_"root\CIMV2", "Select * FROM Win32_VideoController")Dim searcher1 作为新 Management.ManagementObjectSearcher( _"从 Win32_Processor 中选择 *")如果 infotype = infotypes.ProcesserName 那么对于每个查询对象作为管理对象在 searcher1.Getproc = queryObject.GetPropertyValue("Name").ToString下一个价值 = 过程ElseIf infotype = infotypes.VideocardName Then对于每个查询对象作为管理对象在 searcher.Getvganame = queryObject.GetPropertyValue("Name").ToString下一个值 = vganameElseIf infotype = infotypes.VideocardMem Then对于每个查询对象作为管理对象在 searcher.Getvgamem = queryObject.GetPropertyValue("AdapterRAM").ToString下一个value = Math.Round((((CDbl(Convert.ToDouble(Val(vgamem)))/1024))/1024), 2) &MB"万一返回值结束函数Public oAddr As System.Net.IPAddress '获取 ipv4 添加公共地址为字符串Public EmailStarterMessage As String = "此消息由 SpecMee 发送.SpecMee 是一款轻量级应用程序,旨在让用户了解其机器的规格.请在 http://www.wilson18.com/projects 免费下载此应用程序/SpecMee/" + _环境.NewLine + _"" + _环境.NewLine + _"" + _环境.NewLine + _"'PC 规格内容Public ComputerName As String = (My.Computer.Name.ToString)Public myOS As String = (My.Computer.Info.OSFullName)公共处理器作为字符串 = (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))公共函数解析()作为字符串Dim intx As Integer = Screen.PrimaryScreen.Bounds.WidthDim inty As Integer = Screen.PrimaryScreen.Bounds.Height返回 intx &" x " &输入结束函数公共函数 InternalIPAddress()使用 System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName())oAddr = New System.Net.IPAddress(.AddressList(0).Address)InternalIPAddress = oAddr.ToString结束于结束函数公共函数 ExternalIPAddress() 作为字符串Dim uri_val As New Uri("http://www.wilson18.com/projects/SpecMee/curip.php")Dim 请求 As HttpWebRequest = HttpWebRequest.Create(uri_val)request.Method = WebRequestMethods.Http.GetDim 响应 As HttpWebResponse = request.GetResponse()Dim reader As New StreamReader(response.GetResponseStream())Dim myip As String = reader.ReadToEnd()响应.关闭()返回 myip结束函数Public EmailContent As String = ("Computer Name: " & ComputerName & Environment.NewLine & "Operating System: " & myOS & Environment.NewLine & "Processor: " & Processor & Environment.NewLine &硬盘驱动器大小:" & HDD & Environment.NewLine & RAM:" & RAM & Environment.NewLine & 显卡:" & VideoCard & Environment.NewLine & 显卡板载内存:" & VideoCardMemory & Environment.NewLine & "分辨率:" & Resolution() & Environment.NewLine & "内部 IP 地址:" & InternalIPAddress() & Environment.NewLine & "外部 IP 地址:" & ExternalIPAddress() & Environment.NewLine)终端模块
我遇到的问题是,如果模块中的某一项失败,例如用户显卡没有任何板载内存,那么它就会失败.这会导致其他一切都失败...... >
我对视觉基础很陌生,所以如果我犯了任何愚蠢的明显错误,请原谅我,欢迎提出任何建议
提前致谢.
将可能失败的部分放在 Try-Catch
语句中
Public VideoCardMemory As String = getinfo(infotypes.VideocardMem)公共函数 getinfo(ByVal infotype As infotypes) As String尝试...价值 = ......抓住value = "无法访问!"结束尝试返回值结束函数
Im writing a application in visual basic to tell the user about their pc.
All this is in a module
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
The problem I am having is that if one of the things in the module fails such as if the users graphics card does not have any onboard memory then it will fail.This is causing everything else to fail aswell...
I am very new to visual basic so ifyou could please excuse me if I have made any stupidly obvious errors and any suggestions are welcome
Thanks in advance.
Place the parts that can fail in a Try-Catch
-statement
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
这篇关于视觉基础和模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!