2012-06-24 84 views
1

我在Visual Basic中編寫了一個應用程序,告訴用戶他們的電腦。Visual Basic和模塊

這一切都是在一個模塊中

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 

我遇到的問題是,如果在模塊中的事情之一發生故障,例如,如果用戶的顯卡沒有任何板載內存,那麼它會失敗。這是造成一切失敗藏漢...

我是很新的Visual Basic中這樣ifyou可以請原諒我,如果我做任何愚蠢明顯的錯誤和任何建議,歡迎

在此先感謝。

回答

3

地方,可以在失敗份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 
+0

會做的,但反正是有停止這種殺伐整個事情再trycatch? – user1244772

+0

Try-Catch有什麼問題? 'Try-Catch'讓你在可能失敗的呼叫所在的精確位置捕捉異常,從而讓你100%控制你的代碼。代碼執行在異常之後繼續順利進行,並且可以收集下一個系統信息,就好像什麼都沒有發生。 –

+0

這是我需要trycatch出來的事情之一是Public VideoCardMemory As String =(getinfo(infotypes.VideocardMem)),但我不能因爲不在函數中。無論如何,我可以解決這個問題嗎? – user1244772